PDF (美国信纸) - 1.4Mb
PDF (A4) - 1.4Mb
存在一个引用函数来转义 SQL 名称和标识符。 Session.quoteName()
会根据当前连接的设置转义给定的标识符。
注意
引用函数不能用于转义值。请改用 Session.sql()
的值绑定语法;有关一些示例,请参见第 2.4 节,“使用 SQL 与会话”。
MySQL Shell JavaScript 代码
function createTestTable(session, name) {
// use escape function to quote names/identifier
quoted_name = session.quoteName(name);
session.sql("DROP TABLE IF EXISTS " + quoted_name).execute();
var create = "CREATE TABLE ";
create += quoted_name;
create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)";
session.sql(create).execute();
return session.getCurrentSchema().getTable(name);
}
var mysqlx = require('mysqlx');
var session = mysqlx.getSession('user:password@localhost:33060/test');
var default_schema = session.getDefaultSchema().name;
session.setCurrentSchema(default_schema);
// Creates some tables
var table1 = createTestTable(session, 'test1');
var table2 = createTestTable(session, 'test2');
MySQL Shell Python 代码
def createTestTable(session, name):
# use escape function to quote names/identifier
quoted_name = session.quote_name(name)
session.sql("DROP TABLE IF EXISTS " + quoted_name).execute()
create = "CREATE TABLE "
create += quoted_name
create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)"
session.sql(create).execute()
return session.get_current_schema().get_table(name)
from mysqlsh import mysqlx
session = mysqlx.get_session('user:password@localhost:33060/test')
default_schema = session.get_default_schema().name
session.set_current_schema(default_schema)
# Creates some tables
table1 = createTestTable(session, 'test1')
table2 = createTestTable(session, 'test2')
Node.js JavaScript 代码
var mysqlx = require('mysqlx');
function createTestTable(session, name) {
var create = 'CREATE TABLE ';
create += name;
create += ' (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)';
return session
.sql('DROP TABLE IF EXISTS ' + name)
.execute()
.then(function () {
return session.sql(create).execute();
});
}
var session;
mysqlx
.getSession({
user: 'user',
password: 'password'
})
.then(function (s) {
session = s;
return session
.sql('use myschema')
.execute()
})
.then(function () {
// Creates some tables
return Promise.map([
createTestTable(session, 'test1'),
createTestTable(session, 'test2')
])
})
.then(function () {
session.close();
})
});
C# 代码
var session = MySQLX.GetSession("server=localhost;port=33060;user=user;password=password;");
session.SQL("use test;").Execute();
session.GetSchema("test");
// Creates some tables
var table1 = CreateTestTable(session, "test1");
var table2 = CreateTestTable(session, "test2");
private Table CreateTestTable(Session session, string name)
{
// use escape function to quote names/identifier
string quoted_name = "`" + name + "`";
session.SQL("DROP TABLE IF EXISTS " + quoted_name).Execute();
var create = "CREATE TABLE ";
create += quoted_name;
create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)";
session.SQL(create).Execute();
return session.Schema.GetTable(name);
}
Python 代码
# Connector/Python
def createTestTable(session, name):
# use escape function to quote names/identifier
quoted_name = session.quote_name(name)
session.sql("DROP TABLE IF EXISTS " + quoted_name).execute()
create = "CREATE TABLE "
create += quoted_name
create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)"
session.sql(create).execute()
return session.get_current_schema().get_table(name)
from mysqlsh import mysqlx
session = mysqlx.get_session('user:password@localhost:33060/test')
default_schema = session.get_default_schema().name
session.set_current_schema(default_schema)
# Creates some tables
table1 = createTestTable(session, 'test1')
table2 = createTestTable(session, 'test2')
Java 代码
Java does not currently support the quoteName() method.
C++ 代码
#include <mysqlx/xdevapi.h>
// Note: The following features are not yet implemented by
// Connector/C++:
// - DataSoure configuration files,
// - quoteName() method.
Table createTestTable(Session &session, const string &name)
{
string quoted_name = string("`")
+ session.getDefaultSchemaName()
+ L"`.`" + name + L"`";
session.sql(string("DROP TABLE IF EXISTS") + quoted_name).execute();
string create = "CREATE TABLE ";
create += quoted_name;
create += L"(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)";
session.sql(create).execute();
return session.getDefaultSchema().getTable(name);
}
Session session(33060, "user", "password");
Table table1 = createTestTable(session, "test1");
Table table2 = createTestTable(session, "test2");
使用 X DevAPI 的代码不需要转义标识符。这对于使用集合和使用关系表都是如此。