第 6 章 使用關聯式表格

X DevAPI SQL CRUD 函式可讓您以類似於使用傳統 SQL 陳述式的方式來處理關聯式表格。下列程式碼範例示範如何使用 X DevAPI SQL CRUD 函式的 add()select() 方法,這與在具有 SQL 用戶端的表格上執行 INSERTSELECT 陳述式類似。將此與第 4.3 節「集合 CRUD 函式概述」中的範例進行比較,以了解 X DevAPI 中表格和集合的 CRUD 函式之間的差異和相似之處。

// Working with Relational Tables
var mysqlx = require('mysqlx');

// Connect to server using a connection URL
var mySession = mysqlx.getSession( {
  host: 'localhost', port: 33060,
  user: 'user', password: 'password'} )

var myDb = mySession.getSchema('test');

// Accessing an existing table
var myTable = myDb.getTable('my_table');

// Insert SQL Table data
myTable.insert(['name', 'birthday', 'age']).
  values('Laurie', mysqlx.dateValue(2000, 5, 27), 19).execute();

// Find a row in the SQL Table
var myResult = myTable.select(['_id', 'name', 'birthday']).
  where('name like :name AND age < :age').
  bind('name', 'L%').bind('age', 30).execute();

// Print result
print(myResult.fetchOne());