CData Python Connector for OData

Build 20.0.7587

Querying Data

After connecting with the steps in Connecting, you can query data using a session object.

Model Data Using a Mapping Class

Declare a mapping class for the table you wish to model in the ORM. This is done using "sqlalchemy.ext.declarative.declarative_base". A known table in the data model is modeled either partially or completely as in the below example:
import sqlalchemy.ext.declarative.declarative_base
Base = declarative_base()
class Lead(Base):
	__tablename__ = "Lead"
	Id = Column(String, primary_key=True)
	Id = Column(String)
	FullName = Column(String)

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("odata:///?User=myuseraccount;Password=mypassword;URL=http://myserver/myOrgRoot;")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query(Lead).filter_by(FirstName="Bartholomew"):
	print("Id: ", instance.Id)
	print("Id: ", instance.Id)
	print("FullName: ", instance.FullName)

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 should be just as viable:

Lead_table = Lead.metadata.tables["Lead"]
for instance in session.execute( == "Bartholomew")):
	print("Id: ", instance.Id)
	print("FullName: ", instance.Name)
	print("City: ", instance.BillingCity)

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7587