Documentation Home
MySQL Connector/Python Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


11.34 _mysql_connector.MySQL.query() Method

Syntax:

ccnx.query(args)

Executes an SQL statement. The permitted arguments are statement, buffered, raw, and raw_as_string.

ccnx.query('DROP TABLE IF EXISTS t')
ccnx.query('CREATE TABLE t (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY)')
ccnx.query('INSERT INTO t (i) VALUES (NULL),(NULL),(NULL)')
ccnx.query('SELECT LAST_INSERT_ID()')
row = ccnx.fetch_row()
print('LAST_INSERT_ID(): ', row)
ccnx.consume_result()

buffered and raw, if not provided, take their values from the MySQL instance. raw_as_string is a special argument for Python v2 and returns str instead of bytearray (compatible with Connector/Python v1.x).

To check whether the query returns rows, check the have_result_set property of the MySQL instance.

query() returns True if the query executes, and raises an exception otherwise. It raises a TypeError exception if any argument has an invalid type, and a MySQLInterfaceError exception for any MySQL error returned by the MySQL server.