本手册下载

X DevAPI用户指南/使用关系表

第6章使用关系表

表的内容

6.1 SQL CRUD函数

本节解释如何使用X DevAPI SQL CRUD函数处理关系表。

下面的示例代码比较了前面为集合显示的操作,以及如何使用SQL将它们用于处理关系表。在会话中使用SQL演示了简化的X DevAPI语法,并展示了它与处理文档的相似之处。

MySQL Shell JavaScript代码

//使用关系型表var mysqlx = require('mysqlx');//连接服务器使用连接URL var mySession = mysqlx。getSession({host: 'localhost', port: 33060, user: 'user', password: ')密码'}) var myDb = myssession . getschema('测试');//访问一个已存在的表var myTable = myDb.getTable('my_table');//插入SQL表数据myTable。插入([‘名’,‘生日’,‘年龄’])。值(Laurie, mysqlx。dateValue(2000, 5, 27), 19).execute();//在SQL表中查找一行var myResult = myTable。选择([‘_id’,‘名’,‘生日’])。where('name like:name AND age <:age')。绑定('名称',' L % ')。绑定(“年龄”,30). execute (); // Print result print(myResult.fetchOne());

MySQL Shell Python代码

#连接到服务器使用连接URL myssession = mysqlx。Get_session ({'host': 'localhost', 'port': 33060, 'user': 'user', 'password': '密码'}) myDb = myssession .get_schema('test') #访问已存在的表myTable = myDb.get_table('my_table') #插入SQL表数据myTable. Insert (['name','birthday','age']) \ .values('Laurie', mysqlx. get_schema('test'))date_value(2000, 5, 27), 19).execute() #在SQL表中查找一行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())

node . js JavaScript代码

//使用关系型表var mysqlx = require('@mysql/xdevapi');var myTable;// mysqlx .getSession({user: 'user', password: 'password', host: 'localhost', port: 33060}) .then(function (session){//访问现有的表myTable = session. getschema ('test').getTable('my_table');// Insert SQL Table data return myTable .insert(['name', 'birthday', 'age']) .values(['Laurie', '2000-5-27', 19]) .execute()}) .then(function(){//在SQL Table中查找一行返回myTable .select(['_id', 'name', 'birthday']) .where('name like:name && age <:age ')) .bind('name', 'L%') .bind('age', '年龄'),30) .execute(function (row) {console.log(row);});}) .then(函数(myResult) {console.log(myResult);});

c#代码

var db = MySQLX.GetSession("server=localhost;port=33060;user=user密码”).GetSchema(“测试”);//访问一个已经存在的表var myTable = db.GetTable("my_table");//插入SQL表数据myTable。插入(“名字”,“年龄”). values(“劳里”、“19”). execute ();//在SQL表中查找一行var myResult = myTable。Select("_id, name, age") .Where("name like:name AND age <:age") .Bind(new {name = "L%", age = 30}).Execute();//打印结果printreresult (myResult.FetchAll());

Python代码

#连接到服务器使用连接URL my_session = mysqlx。get_session({'host': 'localhost', 'port': 33060, 'user': 'user', 'password': 'password'}) my_schema = my_session.get_schema('test') #访问已存在的表my_table = my_schema.get_table('my_table') # Insert SQL table data my_table. get_table('my_table')插入([‘名’,‘生日’,‘年龄’])\ . values (Laurie, mysqlx。date_value(2000, 5, 27), 19).execute() #在SQL表中查找一行结果= my_table. SQLselect(['_id', 'name', 'birthday']) \ .where('name like:name AND age <:age') \ .bind('name', 'L%') \ .bind('age', 30).execute() # Print result Print (result.fetch_all())

Java代码

//使用关系表import com.mysql.cj.xdevapi.*;//连接服务器使用连接URL Session myssession = new SessionFactory().getSession("mysqlx://localhost:33060/test?user=user&password=password");Schema db = myssession . getschema ("test");//访问已存在的表table myTable = db.getTable("my_table");//插入SQL表数据myTable。插入(“名字”、“生日”,“年龄”)。值(“劳里”、“2000-05-27”、19). execute ();//查找SQL表RowResult myResult = myTable。select("_id ", "name", " birthday") .where("name like:name AND age <:age") .bind("name", "L%")。绑定(“时代”,30). execute ();//打印结果myResult。forEach(r -> System.out.println(r.getString(1) + ": " + r.getDate(2)));

c++代码

//使用关系表#include  //连接到服务器使用连接URL Session myssession (33060, "user", "password");Schema myDb = myssession . getschema ("test");myTable = myDb.getTable("my_table");//插入SQL表数据myTable。插入(“名字”、“生日”,“年龄”). values(“劳里”、“2000-5-27”19). execute ();//查找SQL表RowResult myResult = myTable。选择(“_id”、“名称”、“生日”)。(“名字:姓名和年龄<时代”).bind(“名字”、“L %”)。绑定(“时代”,30). execute ();//打印结果Row Row = myResult.fetchOne();Cout << " _id: " <<行[0]<< endl;Cout << " name: " << row[1] << endl; cout << "birthday: " << row[2] << endl;