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("sage50uk:///?URL=http://localhost:5493/sdata/accounts50/GCRM/{C4C863BE-B098-4A7D-A78B-D7A92B8ADB59};User=Manager;Password=xxxxxx;") factory = sessionmaker(bind=engine) session = factory() for instance in session.query(TradingAccounts).filter_by(TradingAccountUUID="c2ef66a5-a545-413b-9312-79a53caadbc4"): print("TradingAccountUUID: ", instance.TradingAccountUUID) print("TradingAccountUUID: ", instance.TradingAccountUUID) print("Name: ", instance.Name) 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:TradingAccounts_table = TradingAccounts.metadata.tables["TradingAccounts"] for instance in session.execute(TradingAccounts_table.select().where(TradingAccounts_table.c.TradingAccountUUID == "c2ef66a5-a545-413b-9312-79a53caadbc4")): print("Id: ", instance.Id) print("FullName: ", instance.Name) print("City: ", instance.BillingCity) print("---------")