Documentation Home
MySQL Connector/Python Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.6Mb
PDF (A4) - 0.6Mb
HTML Download (TGZ) - 83.7Kb
HTML Download (Zip) - 99.4Kb


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.


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