PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb
X DevAPI 支持许多现代实践,使 CRUD 操作更容易使用,并自然地融入现代开发环境。本节说明如何在使用 Session 对象时使用方法链,而不是使用 JSON 结构的 SQL 字符串。
以下示例显示了在使用 Session 对象时如何使用方法链而不是 SQL 字符串。该示例假定 test
模式和 employee
表都存在。
MySQL Shell 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 Shell 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();