6.1 SQL CRUD 函數的語法

X DevAPI 中提供下列 SQL CRUD 函數。

Table.insert()

Table.insert() 方法的作用類似於 SQL 中的 INSERT 陳述式。它用於將資料儲存在資料庫中的關聯式表格中。它由 execute() 函數執行。

下列範例示範如何使用 Table.insert() 函數。此範例假設 test 綱要存在並已指派給變數 db,且存在一個名為 my_table 的空表格。

# Accessing an existing table
myTable = db.get_table('my_table')

# Insert a row of data.
myTable.insert(['id', 'name']).values(1, 'Imani').values(2, 'Adam').execute()

圖 6.1 Table.insert() 語法圖

Content is described in the surrounding text.

Table.select()

Table.select() 方法的作用類似於 SQL 中的 SELECT 陳述式。請注意,Table.select()collection.find() 使用不同的方法來排序結果:Table.select() 使用 orderBy() 方法,讓人聯想到 SQL 中的 ORDER BY 關鍵字,而 sort() 方法則用於排序 Collection.find() 傳回的結果。

圖 6.2 Table.select() 語法圖

Content is described in the surrounding text.

Table.update()

Table.update() 方法的作用類似於 SQL 中的 UPDATE 陳述式。

圖 6.3 Table.update() 語法圖

Content is described in the surrounding text.

Table.delete()

Table.delete() 方法的作用類似於 SQL 中的 DELETE 陳述式。

圖 6.4 Table.delete() 語法圖

Content is described in the surrounding text.