PDF (美式信紙) - 1.2Mb
PDF (A4) - 1.2Mb
X DevAPI SQL CRUD 函式可讓您以類似於使用傳統 SQL 陳述式的方式使用關聯式表格。下列程式碼範例示範如何使用 X DevAPI SQL CRUD 函式的 add()
和 select()
方法,這些方法類似於在具有 SQL 用戶端的表格上執行 INSERT
和 SELECT
陳述式。將此範例與第 4.3 節「集合 CRUD 函式概觀」中的範例進行比較,以查看 X DevAPI 中表格和集合的 CRUD 函式之間的差異和相似之處。
# Working with Relational Tables
from mysqlsh import mysqlx
# Connect to server using a connection URL
mySession = mysqlx.get_session( {
'host': 'localhost', 'port': 33060,
'user': 'user', 'password': 'password'} )
myDb = mySession.get_schema('test')
# Accessing an existing table
myTable = myDb.get_table('my_table')
# Insert SQL Table data
myTable.insert(['name','birthday','age']) \
.values('Laurie', mysqlx.date_value(2000, 5, 27), 19).execute()
# Find a row in the SQL Table
myResult = myTable.select(['_id', 'name', 'birthday']) \
.where('name like :name AND age < :age') \
.bind('name', 'L%') \
.bind('age', 30).execute()
# Print result
print(myResult.fetch_all())