X DevAPI User Guide for MySQL Shell in Python Mode

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.

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")

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.