All previous examples used the getSchema()
or
getDefaultSchema()
methods of the Session
object, which return a Schema object. You use this Schema object
to access Collections and Tables. Most examples make use of the
X DevAPI ability to chain all object constructions, enabling you
to get to the Schema object in one line. For example:
Press CTRL+C to copyschema = mysqlx.getSession(...).getSchema();
This object chain is equivalent to the following, with the difference that the intermediate step is omitted:
Press CTRL+C to copysession = mysqlx.getSession(); schema = session.getSchema().
There is no requirement to always chain calls until you get a
Schema object, neither is it always what you want. If you want to
work with the Session object, for example, to call the Session
object method getSchemas()
, there is no need to
navigate down to the Schema. For example:
Press CTRL+C to copysession = mysqlx.getSession(); session.getSchemas().
In this example the mysqlx.getSession()
function is used to open a Session. Then the
Session.getSchemas()
function is used to get a
list of all available schemas and print them to the console.
MySQL Shell JavaScript Code
Press CTRL+C to copy// 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 Code
Press CTRL+C to copy# 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 Code
Press CTRL+C to copy// 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# Code
Press CTRL+C to copy// 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 Code
Press CTRL+C to copy# 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 Code
Press CTRL+C to copyimport 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++ Code
Press CTRL+C to copy#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; } }