Documentation Home
X DevAPI User Guide for MySQL Shell in Python Mode
Download this Manual
PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb


2.6 Dynamic SQL

A quoting function exists to escape SQL names and identifiers. Session.quoteName() escapes the identifier given in accordance to the settings of the current connection.

Note

The quoting function must not be used to escape values. Use the value binding syntax of Session.sql() instead; see Section 2.4, “Using SQL with Session” for some examples.

def createTestTable(session, name):

    # use escape function to quote names/identifier
    quoted_name = session.quote_name(name)
    session.sql("DROP TABLE IF EXISTS " + quoted_name).execute()
    create = "CREATE TABLE "
    create += quoted_name
    create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)"
    session.sql(create).execute()
    return session.get_current_schema().get_table(name)

from mysqlsh import mysqlx

session = mysqlx.get_session('user:password@localhost:33060/test')

default_schema = session.get_default_schema().name
session.set_current_schema(default_schema)

# Creates some tables
table1 = createTestTable(session, 'test1')
table2 = createTestTable(session, 'test2')

Code that uses X DevAPI does not need to escape identifiers. This is true for working with collections and for working with relational tables.