PDF (美式信紙) - 1.2Mb
PDF (A4) - 1.2Mb
先前的所有範例都使用了 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 的清單,並將其列印至主控台。
# 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()