PDF (美式信紙) - 1.4Mb
PDF (A4) - 1.4Mb
先前的所有範例都使用了 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()
函式來取得所有可用 Schema 的清單,並將其列印到主控台。
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;
}
}