Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  ...  /  Setting the Current Schema

Pre-General Availability Draft: 2018-02-17

2.5.2 Setting the Current Schema

A newly opened session has no schema set. There is no concept of an implicitly set default schema when a session is established. A default schema can be defined, but a session's current schema is not set to a default before being explicitly selected.

An explicit schema selection happens as part of using the CRUD functions of the X DevAPI. For example, Table and Collection objects are created by Schema objects. Schema objects, in turn, are created by Session objects. To insert rows into a table, call session.getSchema().getTable().insert(). To add a document to a collection from the default schema, call session.getDefaultSchema().getCollection().add().

The sql() function is a method of the NodeSession class but not the Schema class. Upon creation, the NodeSession class has no schema selected by default. Therefore, the sql() function does not know which schema to execute the SQL statement against. Use the Session class setCurrentSchema() function to set or change the current schema.

MySQL Shell JavaScript Code

// Direct connect with no client side default schema defined
var mySession = mysqlx.getNodeSession('mike:s3cr3t!@localhost');

MySQL Shell Python Code

import mysqlx

# Direct connect with no client side default schema defined
mySession = mysqlx.get_node_session('mike:s3cr3t!@localhost')

Node.js JavaScript Code

C# Code

// Direct connect with no client side default schema defined
var mySession = MySQLX.GetNodeSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");
var session = MySQLX.GetNodeSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");

var default_schema = session.GetSchema("test");

// print the current schema name

private Table CreateTestTable(NodeSession session, string name)

// use escape function to quote names/identifier
string quoted_name = "`" + name + "`";

session.SQL("DROP TABLE IF EXISTS " + quoted_name).Execute();

var create = "CREATE TABLE ";
create += quoted_name;


return session.Schema.GetTable(name);

Java Code

import com.mysql.cj.api.xdevapi.*;
import com.mysql.cj.xdevapi.*;

// Schema 'test' set in connection URL
NodeSession mySession = new XSessionFactory().getNodeSession("mysqlx://localhost:33060/test?user=mike&password=s3cr3t!");

C++ Code

#include <mysql_devapi.h>

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

string url = "mysqlx://localhost:33060/test?user=mike&password=s3cr3t!"
NodeSession mySession(url);

C Code

User Comments
Sign Up Login You must be logged in to post a comment.