CData Python Connector for Google Spanner

Build 24.0.9060

Querying Data

After you use the steps in Connecting to connect, and use one of the methods in Reflecting Metadata to reflect some of the metadata, you can use a session object to query data.

Querying Data Using the Query Method

If the mapping class has been prepared, use it with a session object to query the data source. After binding the engine to the session, provide the mapping class to the session's query method.

For example:

engine = create_engine("googlespanner:///?ProjectId='project1';InstanceId='instance1';Database='db1';OAuthClientId='757060765381';OauthClientSecret='abc';")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query([CData].[GoogleSpanner].Customer).filter_by(CustomerId="12345"):
	print("Id: ", instance.Id)
	print("Name: ", instance.Name)
	print("TotalDue: ", instance.TotalDue)
	print("---------")

Querying Data Using the Execute Method

The session object can also run the query with the execute() method alongside the appropriate Table object. Assuming you have an active session, the following is just as viable:
[CData].[GoogleSpanner].Customer_table = [CData].[GoogleSpanner].Customer.metadata.tables["[CData].[GoogleSpanner].Customer"]
for instance in session.execute([CData].[GoogleSpanner].Customer_table.select().where([CData].[GoogleSpanner].Customer_table.c.CustomerId == "12345")):
	print("Id: ", instance.Id)
	print("FullName: ", instance.Name)
	print("City: ", instance.BillingCity)
	print("---------")

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060