文档主页
X DevAPI 用户指南
下载本手册

X DevAPI 用户指南  /  连接和会话概念  /  使用 Session 对象

2.3 使用 Session 对象

之前的所有示例都使用了 Session 对象的 getSchema()getDefaultSchema() 方法,它们返回一个 Schema 对象。您可以使用此 Schema 对象访问集合和表。大多数示例都利用了 X DevAPI 将所有对象构造链接在一起的能力,使您能够在一行中获得 Schema 对象。例如

schema = mysqlx.getSession(...).getSchema();

此对象链等效于以下内容,区别在于省略了中间步骤

session = mysqlx.getSession(); 
schema = session.getSchema().

没有要求始终链接调用,直到您获得 Schema 对象,也不一定是您想要的。例如,如果您想使用 Session 对象调用 Session 对象方法 getSchemas(),则无需导航到 Schema。例如

session = mysqlx.getSession(); session.getSchemas().

在此示例中,mysqlx.getSession() 函数用于打开一个 Session。然后,使用 Session.getSchemas() 函数获取所有可用模式的列表并将其打印到控制台。

MySQL Shell 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 Shell 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://127.0.0.1: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://127.0.0.1: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;
  }
}