Documentation Home
X DevAPI User Guide
Download this Manual

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

2.6 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 runSql() function is a method of the Session class but not the Schema class. Upon creation, the Session class has no schema selected by default. Therefore, the runSql() 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

var mysqlx = require('mysqlx');

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

MySQL Shell Python Code

from mysqlsh import mysqlx

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

Node.js JavaScript Code

C# Code

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

Python Code

# Connector/Python
from mysqlsh import mysqlx

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

Java Code

import com.mysql.cj.xdevapi.*;

// Schema 'test' set in connection URL
Session mySession = new SessionFactory().getSession("mysqlx://localhost:33060/test?user=user&password=password");

C++ Code

#include <mysqlx/xdevapi.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=user&password=password"
Session mySession(url);

C Code


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