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('user: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('user: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=user;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('user:password@localhost')
mySession.set_current_schema("test")
Java Code
import com.mysql.cj.xdevapi.*;
// Schema 'test' set in connection URI
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