CData Python Connector for EnterpriseDB

Build 22.0.8479

Other SQL Clauses

SQLAlchemy ORM は、ORDER BY、GROUP BY、LIMIT、OFFSET などSQL の他の句のサポートも公開しています。これらはすべて、このコネクタでサポートされています。

ORDER BY

以下の例は、セッションオブジェクトのquery() メソッドを使用して指定したカラムでソートします。

rs = session.query("postgres"."public".Orders).order_by("postgres"."public".Orders.Freight)
for instance in rs:
	print("Id: ", instance.Id)
	print("ShipName: ", instance.ShipName)
	print("ShipCity: ", instance.ShipCity)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してORDER BY を実行することもできます。

rs = session.execute("postgres"."public".Orders_table.select().order_by("postgres"."public".Orders_table.c.Freight))
for instance in rs:

GROUP BY

以下の例は、セッションオブジェクトのquery() メソッドを使用して指定したカラムを持つレコードをグループ化します。

rs = session.query(func.count("postgres"."public".Orders.Id).label("CustomCount"), "postgres"."public".Orders.ShipName).group_by("postgres"."public".Orders.ShipName)
for instance in rs:
	print("Count: ", instance.CustomCount)
	print("ShipName: ", instance.ShipName)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してGROUP BY を実行することもできます。

rs = session.execute("postgres"."public".Orders_table.select().with_only_columns([func.count("postgres"."public".Orders_table.c.Id).label("CustomCount"), "postgres"."public".Orders_table.c.ShipName]).group_by("postgres"."public".Orders_table.c.ShipName))
for instance in rs:

LIMIT およびOFFSET

以下の例は、最初の100レコードをスキップし、セッションオブジェクトのquery() メソッドを使用して次の25レコードをフェッチします。

rs = session.query("postgres"."public".Orders).limit(25).offset(100)
for instance in rs:
	print("Id: ", instance.Id)
	print("ShipName: ", instance.ShipName)
	print("ShipCity: ", instance.ShipCity)
	print("---------")

あるいは、セッションオブジェクトのexecute() メソッドを使用してLIMIT またはOFFSET を設定することもできます。

rs = session.execute("postgres"."public".Orders_table.select().limit(25).offset(100))
for instance in rs:

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479