第 6 章 使用关系表

X DevAPI SQL CRUD 函数允许您以类似于使用传统 SQL 语句的方式处理关系表。以下代码示例展示了如何使用 X DevAPI SQL CRUD 函数的add()select()方法,这类似于使用 SQL 客户端在表上运行INSERTSELECT语句。将此与第 4.3 节“集合 CRUD 函数概述”中的示例进行比较, 以了解 X DevAPI 中表和集合的 CRUD 函数之间的异同。

Press CTRL+C to copy
// 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());