X DevAPI 用户指南 / 第 2 章连接和会话概念 /
2.3 使用会话对象
之前的所有示例都使用了Session 对象的getSchema()
或
getDefaultSchema()
方法,它们返回一个 Schema 对象。您使用此 Schema 对象来访问 Collections 和 Tables。大多数示例利用 X DevAPI 的能力来链接所有对象构造,使您能够在一行中获得架构对象。例如:
schema = mysqlx.getSession(...).getSchema();
此对象链等同于以下内容,不同之处在于省略了中间步骤:
session = mysqlx.getSession();
schema = session.getSchema().
在您获得 Schema 对象之前,不需要始终进行链式调用,它也不总是您想要的。如果要使用 Session 对象,例如调用 Session 对象方法getSchemas()
,则无需向下导航到 Schema。例如:
session = mysqlx.getSession(); session.getSchemas().
在此示例中,该mysqlx.getSession()
函数用于打开会话。然后该
Session.getSchemas()
函数用于获取所有可用模式的列表并将它们打印到控制台。
MySQL 外壳 JavaScript 代码
// Connecting to MySQL and working with a Session
var mysqlx = require('mysqlx');
// Connect to a dedicated MySQL server using a connection URI
var mySession = mysqlx.getSession('user:password@localhost');
// Get a list of all available schemas
var schemaList = mySession.getSchemas();
print('Available schemas in this session:\n');
// Loop over all available schemas and print their name
for (index in schemaList) {
print(schemaList[index].name + '\n');
}
mySession.close();
MySQL 外壳 Python 代码
# Connecting to MySQL and working with a Session
from mysqlsh import mysqlx
# Connect to a dedicated MySQL server using a connection URI
mySession = mysqlx.get_session('user:password@localhost')
# Get a list of all available schemas
schemaList = mySession.get_schemas()
print('Available schemas in this session:\n')
# Loop over all available schemas and print their name
for schema in schemaList:
print('%s\n' % schema.name)
mySession.close()
Node.js JavaScript 代码
// Connecting to MySQL and working with a Session
var mysqlx = require('@mysql/xdevapi');
// Connect to a dedicated MySQL server using a connection URI
mysqlx
.getSession('user:password@localhost')
.then(function (mySession) {
// Get a list of all available schemas
return mySession.getSchemas();
})
.then(function (schemaList) {
console.log('Available schemas in this session:\n');
// Loop over all available schemas and print their name
schemaList.forEach(function (schema) {
console.log(schema.getName() + '\n');
});
});
C#代码
// Connect to a dedicated MySQL server node using a connection URI
var mySession = MySQLX.GetSession("mysqlx://user:password@localhost:33060");
// Get a list of all available schemas
var schemaList = mySession.GetSchemas();
Console.WriteLine("Available schemas in this session:");
// Loop over all available schemas and print their name
foreach (var schema in schemaList)
{
Console.WriteLine(schema.Name);
}
mySession.Close();
Python代码
# Connector/Python
# Connecting to MySQL and working with a Session
from mysqlsh import mysqlx
# Connect to a dedicated MySQL server using a connection URI
mySession = mysqlx.get_session('user:password@localhost')
# Get a list of all available schemas
schemaList = mySession.get_schemas()
print('Available schemas in this session:\n')
# Loop over all available schemas and print their name
for schema in schemaList:
print('%s\n' % schema.name)
mySession.close()
Java代码
import java.util.List;
import com.mysql.cj.xdevapi.*;
// Connecting to MySQL and working with a Session
// Connect to a dedicated MySQL server using a connection URI
Session mySession = new SessionFactory().getSession("mysqlx://localhost:33060/test?user=user&password=password");
// Get a list of all available schemas
List<Schema> schemaList = mySession.getSchemas();
System.out.println("Available schemas in this session:");
// Loop over all available schemas and print their name
for (Schema schema : schemaList) {
System.out.println(schema.getName());
}
mySession.close();
C++代码
#include <mysqlx/xdevapi.h>
// Connecting to MySQL and working with a Session
// Connect to a dedicated MySQL server using a connection URI
string url = "mysqlx://localhost:33060/test?user=user&password=password";
{
Session mySession(url);
// Get a list of all available schemas
std::list<Schema> schemaList = mySession.getSchemas();
cout << "Available schemas in this session:" << endl;
// Loop over all available schemas and print their name
for (Schema schema : schemaList) {
cout << schema.getName() << endl;
}
}