CData Python Connector for Greenplum

Build 21.0.7930

Querying Data

After connecting with the steps in Connecting, and reflecting some of the metadata with one of the methods in Reflecting Metadata, you can query data using a session object.

Querying Data Using the Query Method

If the mapping class has been prepared, it is used alongside a session object to query the data source. After binding the engine to the session, simply provide the mapping class to the session's query method, as below:

engine = create_engine("cdata_greenplum:///?User=user;Password=admin;Database=dbname;Server=127.0.0.1;Port=5432")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query("template1"."public".Orders).filter_by(ShipCountry="USA"):
	print("Id: ", instance.Id)
	print("ShipName: ", instance.ShipName)
	print("ShipCity: ", instance.ShipCity)
	print("---------")

Querying Data Using the Execute Method

As an alternative, the session object can also run the query with the execute() method alongside the appropriate Table object. Assuming you have an active session, the below is just as viable:

"template1"."public".Orders_table = "template1"."public".Orders.metadata.tables["\"template1\".\"public\".Orders"]
for instance in session.execute("template1"."public".Orders_table.select().where("template1"."public".Orders_table.c.ShipCountry == "USA")):
	print("Id: ", instance.Id)
	print("FullName: ", instance.Name)
	print("City: ", instance.BillingCity)
	print("---------")

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 21.0.7930