データのクエリ
接続 の手順を使用して接続し、メタデータの反映 メソッドのいずれかを使用してメタデータの一部を反映させたら、セッションオブジェクトを使用してデータをクエリできます。
query メソッドを使用したデータのクエリ
マッピングクラスが用意されている場合は、セッションオブジェクトと併用してデータソースをクエリします。エンジンをセッションにバインドしたら、マッピングクラスをセッションのクエリメソッドに提供します。次に例を示します。
engine = create_engine("authorizenet:///?LoginID=myLoginID;TransactionKey=myTransactionKey") factory = sessionmaker(bind=engine) session = factory() for instance in session.query(SettledBatchList).filter_by(IncludeStatistics="True"): print("Id: ", instance.Id) print("TotalCharge: ", instance.TotalCharge) print("Product: ", instance.Product) print("---------")
execute メソッドを使用したデータのクエリ
セッションオブジェクトは、適切なテーブルオブジェクトと一緒にexecute() メソッドを使用してクエリを実行することもできます。アクティブなセッションがあると仮定すると、以下は同様に実行可能です。SettledBatchList_table = SettledBatchList.metadata.tables["SettledBatchList"] for instance in session.execute(SettledBatchList_table.select().where(SettledBatchList_table.c.IncludeStatistics == "True")): print("Id: ", instance.Id) print("FullName: ", instance.Name) print("City: ", instance.BillingCity) print("---------")