X DevAPI 用户指南 / 第三章CRUD操作 /
3.2 方法链接
X DevAPI 支持许多现代实践,使使用 CRUD 操作更容易,并自然地适应现代开发环境。本节介绍如何使用方法链接而不是使用 JSON 结构的 SQL 字符串。
以下示例显示了在使用 Session 对象时如何使用方法链接而不是 SQL 字符串。该示例假定test
模式存在并且
employee
表存在。
MySQL 外壳 JavaScript 代码
// New method chaining used for executing an SQL SELECT statement
// Recommended way for executing queries
var employees = db.getTable('employee');
var res = employees.select(['name', 'age']).
where('name like :param').
orderBy(['name']).
bind('param', 'm%').execute();
// Traditional SQL execution by passing an SQL string
// It should only be used when absolutely necessary
var result = session.sql('SELECT name, age ' +
'FROM employee ' +
'WHERE name like ? ' +
'ORDER BY name').bind('m%').execute();
MySQL 外壳 Python 代码
# New method chaining used for executing an SQL SELECT statement
# Recommended way for executing queries
employees = db.get_table('employee')
res = employees.select(['name', 'age']) \
.where('name like :param') \
.order_by(['name']) \
.bind('param', 'm%').execute()
# Traditional SQL execution by passing an SQL string
# It should only be used when absolutely necessary
result = session.sql('SELECT name, age ' +
'FROM employee ' +
'WHERE name like ? ' +
'ORDER BY name').bind('m%').execute()
Node.js JavaScript 代码
// New method chaining used for executing an SQL SELECT statement
// Recommended way for executing queries
var employees = db.getTable('employee');
var promise = employees.select('name', 'age')
.where('name like :name')
.orderBy('name')
.bind('m%')
.execute();
// Traditional SQL execution by passing an SQL string
var sqlString = 'SELECT name, age ' +
'FROM employee ' +
'WHERE name like ? ' +
'ORDER BY name';
var promise = db.executeSql(sqlString, 'm%').execute();
C#代码
// New method chaining used for executing an SQL SELECT statement
// Recommended way for executing queries
var employees = db.GetTable("employee");
var res = employees.Select("name", "age")
.Where("name like :param")
.OrderBy("name")
.Bind("param", "m%").Execute();
// Traditional SQL execution by passing an SQL string
// It should only be used when absolutely necessary
var result = session.SQL("SELECT name, age " +
"FROM employee " +
"WHERE name like ? " +
"ORDER BY name").Bind("m%").Execute();
Python代码
# Connector/Python
# New method chaining used for executing an SQL SELECT statement
# Recommended way for executing queries
employees = db.get_table('employee')
res = employees.select(['name', 'age']) \
.where('name like :param') \
.order_by(['name']) \
.bind('param', 'm%').execute()
# Traditional SQL execution by passing an SQL string
# It should only be used when absolutely necessary
result = session.sql('SELECT name, age ' +
'FROM employee ' +
'WHERE name like ? ' +
'ORDER BY name').bind('m%').execute()
Java代码
// New method chaining used for executing an SQL SELECT statement
// Recommended way for executing queries
Table employees = db.getTable("employee");
RowResult res = employees.select("name, age")
.where("name like :param")
.orderBy("name")
.bind("param", "m%").execute();
// Traditional SQL execution by passing an SQL string
// It should only be used when absolutely necessary
SqlResult result = session.sql("SELECT name, age " +
"FROM employee " +
"WHERE name like ? " +
"ORDER BY name").bind("m%").execute();
C++代码
// New method chaining used for executing an SQL SELECT statement
// Recommended way for executing queries
Table employees = db.getTable("employee");
RowResult res = employees.select("name", "age")
.where("name like :param")
.orderBy("name")
.bind("param", "m%").execute();
// Traditional SQL execution by passing an SQL string
// It should only be used when absolutely necessary
RowResult result = session.sql("SELECT name, age "
"FROM employee "
"WHERE name like ? "
"ORDER BY name").bind("m%").execute();