Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  Connection and Session Concepts  /  Setting the Current Schema

2.5 Setting the Current Schema

A default schema for a session can be specified using the schema attribute in the URI-like connection string or key-value pairs when opening a connection session. The Session class getDefaultSchema() method returns the default schema for the Session.

If no default schema has been selected at connection, the Session class setCurrentSchema() function can be used to set a current schema.

MySQL Shell JavaScript Code

var mysqlx = require('mysqlx');

// Direct connect with no client-side default schema specified
var mySession = mysqlx.getSession('user:password@localhost');
mySession.setCurrentSchema("test");

MySQL Shell Python Code

from mysqlsh import mysqlx

# Direct connect with no client-side default schema specified
mySession = mysqlx.get_session('user:password@localhost')
mySession.set_current_schema("test")

Node.js JavaScript Code

/*
  Connector/Node.js does not support the setCurrentSchema() method.
  One can specify the default schema in the connection string.
*/

C# Code

// Direct connect with no client-side default schema specified
var mySession = MySQLX.GetSession("server=localhost;port=33060;user=user;password=password;");
mySession.SetCurrentSchema("test");

Python Code

# Connector/Python
from mysqlsh import mysqlx

# Direct connect with no client-side default schema specified
mySession = mysqlx.get_session('user:password@localhost')
mySession.set_current_schema("test")

Java Code

/*
  Connector/J does not support the setCurrentSchema() method.
  One can specify the default schema in the connection string.
*/

C++ Code

/*
  Connector/C++ does not support the setCurrentSchema() method.
  One can specify the default schema in the connection string.
*/

Notice that setCurrentSchema() does not change the session's default schema, which remains unchanged throughout the session, or remains null if not set at connection. The schema set by setCurrentSchema() can be returned by the getCurrentSchema() method.

An alternative way to set the current schema is to use the Session class sql() method and the USE db_name statement.