MySQL Connector/Python

Abstract

This manual describes MySQL Connector/Python.

Document generated on: 2012-07-17

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

If this software or related documentation is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle USA, Inc., 500 Oracle Parkway, Redwood City, CA 94065.

This software is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications which may create a risk of personal injury. If you use this software in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure the safe use of this software. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software in dangerous applications.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. MySQL is a trademark of Oracle Corporation and/or its affiliates, and shall not be used without Oracle's express written authorization. Other names may be trademarks of their respective owners.

This software and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.

This documentation is in prerelease status and is intended for demonstration and preliminary use only. It may not be specific to the hardware on which you are using the software. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to this documentation and will not be responsible for any loss, costs, or damages incurred due to the use of this documentation.

The information contained in this document is for informational sharing purposes only and should be considered in your capacity as a customer advisory board member or pursuant to your beta trial agreement only. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described in this document remains at the sole discretion of Oracle.

This document in any form, software or printed matter, contains proprietary information that is the exclusive property of Oracle. Your access to and use of this material is subject to the terms and conditions of your Oracle Software License and Service Agreement, which has been executed and with which you agree to comply. This document and information contained herein may not be disclosed, copied, reproduced, or distributed to anyone outside Oracle without prior written consent of Oracle or as specifically provided below. This document is not part of your license agreement nor can it be incorporated into any contractual agreement with Oracle or its subsidiaries or affiliates.

This documentation is NOT distributed under a GPL license. Use of this documentation is subject to the following terms:

You may create a printed copy of this documentation solely for your own personal use. Conversion to other formats is allowed as long as the actual content is not altered or edited in any way. You shall not publish or distribute this documentation in any form or on any media, except if you distribute the documentation in a manner similar to how Oracle disseminates it (that is, electronically for download on a Web site with the software) or on a CD-ROM or similar medium, provided however that the documentation is disseminated together with the software on the same medium. Any other use, such as any dissemination of printed copies or use of this documentation, in whole or in part, in another publication, requires the prior written consent from an authorized representative of Oracle. Oracle and/or its affiliates reserve any and all rights to this documentation not expressly granted above.

For more information on the terms of this license, or for details on how the MySQL documentation is built and produced, please visit MySQL Contact & Questions.

For additional licensing information, including licenses for third-party libraries used by MySQL products, see Preface and Legal Notice.

For help with using MySQL, please visit either the MySQL Forums or MySQL Mailing Lists where you can discuss your issues with other MySQL users.

For additional documentation on MySQL products, including translations of the documentation into other languages, and downloadable versions in variety of formats, including HTML and PDF formats, see the MySQL Documentation Library.


Table of Contents

MySQL Connector/Python
1. Connector/Python Tutorials
1.1. Tutorial: Raise employee's salary using a buffering cursor
2. Connector/Python Coding Examples
2.1. Connecting to MySQL Using Connector/Python
2.2. Creating Tables Using Connector/Python
2.3. Inserting Data Using Connector/Python
2.4. Selecting Data Using Connector/Python
3. Connector/Python Connection Arguments
4. Connector/Python API Reference
4.1. Errors and Exceptions
4.1.1. Module errorcode
4.1.2. Exception errors.Error
4.1.3. Exception errors.Warning
4.1.4. Exception errors.InterfaceError
4.1.5. Exception errors.DatabaseError
4.1.6. Exception errors.InternalError
4.1.7. Exception errors.OperationalError
4.1.8. Exception errors.ProgrammingError
4.1.9. Exception errors.IntegrityError
4.1.10. Exception errors.DataError
4.1.11. Exception errors.NotSupportedError
4.2. Class connection.MySQLConnection
4.2.1. Constructor connection.MySQLConnection(**kwargs)
4.2.2. Method MySQLConnection.close()
4.2.3. Method MySQLConnection.config(**kwargs)
4.2.4. Method MySQLConnection.connect(**kwargs)
4.2.5. Method MySQLConnection.commit()
4.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)
4.2.7. Method MySQLConnection.cmd_change_user(username='', password='', database='', charset=33)
4.2.8. Method MySQLConnection.cmd_debug()
4.2.9. Method MySQLConnection.cmd_init_db(database)
4.2.10. Method MySQLConnection.cmd_ping()
4.2.11. Method MySQLConnection.cmd_process_info()
4.2.12. Method MySQLConnection.cmd_process_kill(mysql_pid)
4.2.13. Method MySQLConnection.cmd_quit()
4.2.14. Method MySQLConnection.cmd_query(statement)
4.2.15. Method MySQLConnection.cmd_query_iter(statement)
4.2.16. Method MySQLConnection.cmd_refresh(options)
4.2.17. Method MySQLConnection.cmd_shutdown()
4.2.18. Method MySQLConnection.cmd_statistics()
4.2.19. Method MySQLConnection.disconnect()
4.2.20. Method MySQLConnection.get_rows(count=None)
4.2.21. Method MySQLConnection.get_row()
4.2.22. Method MySQLConnection.get_server_info()
4.2.23. Method MySQLConnection.get_server_version()
4.2.24. Method MySQLConnection.is_connected()
4.2.25. Method MySQLConnection.isset_client_flag(flag)
4.2.26. Method MySQLConnection.ping(attempts=1, delay=0)
4.2.27. Method MySQLConnection.reconnect(attempts=1, delay=0)
4.2.28. Method MySQLConnection.rollback()
4.2.29. Method MySQLConnection.set_charset_collation(charset=None, collation=None)
4.2.30. Method MySQLConnection.set_client_flags(flags)
4.2.31. Property MySQLConnection.autocommit
4.2.32. Property MySQLConnection.charset_name
4.2.33. Property MySQLConnection.collation_name
4.2.34. Property MySQLConnection.connection_id
4.2.35. Property MySQLConnection.database
4.2.36. Property MySQLConnection.get_warnings
4.2.37. Property MySQLConnection.raise_on_warnings
4.2.38. Property MySQLConnection.server_host
4.2.39. Property MySQLConnection.server_port
4.2.40. Property MySQLConnection.sql_mode
4.2.41. Property MySQLConnection.time_zone
4.2.42. Property MySQLConnection.unix_socket
4.2.43. Property MySQLConnection.user
4.3. Class cursor.MySQLCursor
4.3.1. Constructor cursor.MySQLCursor
4.3.2. Method MySQLCursor.callproc(procname, args=())
4.3.3. Method MySQLCursor.close()
4.3.4. Method MySQLCursor.execute(operation, params=None, multi=False)
4.3.5. Method MySQLCursor.executemany(operation, seq_params)
4.3.6. Method MySQLCursor.fetchall()
4.3.7. Method MySQLCursor.fetchmany(size=1)
4.3.8. Method MySQLCursor.fetchone()
4.3.9. Method MySQLCursor.fetchwarnings()
4.3.10. Method MySQLCursor.stored_results()
4.3.11. Property MySQLCursor.column_names
4.3.12. Property MySQLCursor.statement
4.3.13. Property MySQLCursor.with_rows
4.4. Class cursor.MySQLCursorBuffered
4.5. Class constants.ClientFlag
4.6. Class constants.FieldType
4.7. Class constants.SQLMode
4.8. Class constants.CharacterSet
4.9. Class constants.RefreshOption

MySQL Connector/Python

MySQL Connector/Python enables creation of Python applications which need to connect with the MySQL Server. It is written in 'pure' Python and does not have any dependencies except for the Python Standard Library.

MySQL Connector/Python is compliant with the Python Database API Specification v2.0 or DB API v2.

MySQL Connector/Python includes support for:

  • Almost all features provided by MySQL Server up to and including MySQL Server version 5.5.
  • Protocol compression, which enables compressing the data stream between the client and server.
  • Connections using TCP/IP sockets and on Unix using Unix sockets.
  • Secure TCP/IP connections using SSL.
  • Fully managed, Connector/Python does not utilize the MySQL client library.

MySQL Connector/Python supports Python version v2.4 till v2.7 and Python v3.1 and later. Note that Connector/Python does not support the old MySQL Server authentication methods, which means that MySQL versions prior to 4.1 will not work.

Chapter 1. Connector/Python Tutorials

These tutorials illustrate how to develop Python applications and scripts which connect to a MySQL Server using MySQL Connector/Python.

1.1. Tutorial: Raise employee's salary using a buffering cursor

The following example script will give a long overdue raise effective tomorrow to all employees who joined in the year 2000 and are still with the company.

We are using buffered cursors to iterate through the selected employees. This way we do not have to fetch the rows in a new variables, but can instead use the cursor as an iterator.

Note that the script is an example, and there are indeed other ways of doing this simple task.

from __future__ import print_function
from decimal import Decimal
from datetime import datetime, date, timedelta
import mysql.connector
# Connect with the MySQL Server
cnx = mysql.connector.connect(user='scott', database='employees')
# Get two buffered cursors
curA = cnx.cursor(buffered=True)
curB = cnx.cursor(buffered=True)
# Query to get employees who joined in a period defined by two dates
query = (
  "SELECT s.emp_no, salary, from_date, to_date FROM employees AS e "
  "LEFT JOIN salaries AS s USING (emp_no) "
  "WHERE to_date = DATE('9999-01-01')"
  "AND e.hire_date BETWEEN DATE(%s) AND DATE(%s)")
# UPDATE and INSERT statements for the old and new salary
update_old_salary = (
  "UPDATE salaries SET to_date = %s "
  "WHERE emp_no = %s AND from_date = %s")
insert_new_salary = (
  "INSERT INTO salaries (emp_no, from_date, to_date, salary) "
  "VALUES (%s, %s, %s, %s)")
  
# Select the employes getting a raise
curA.execute(query, (date(2000, 1, 1), date(2001, 1, 1)))
# Iterate through the result of curA
for (emp_no, salary, from_date, to_date) in curA:
  # Update the old and insert the new salary
  new_salary = int(round(salary * Decimal('1.15')))
  curB.execute(update_old_salary, (tomorrow, emp_no, from_date))
  curB.execute(insert_new_salary,
               (emp_no, tomorrow, date(9999, 1, 1,), new_salary))
  # Commit the changes
  cnx.commit()
cnx.close()
    

Chapter 2. Connector/Python Coding Examples

These coding examples illustrate how to develop Python applications and scripts which connect to a MySQL Server using MySQL Connector/Python.

2.1. Connecting to MySQL Using Connector/Python

The connect() constructor is used for creating a connection to the MySQL server and returns a MySQLConnection object.

The following example shows how to connect to the MySQL server:

import mysql.connector
cnx = mysql.connector.connect(user='scott', password='tiger',
                              host='127.0.0.1',
                              database='employees')
cnx.close()

See Chapter 3, Connector/Python Connection Arguments for all possible connection arguments.

It is also possible to create connection objects using the connection.MySQLConnection() class. Both methods, using the connect() constructor, or the class directly, are valid and functionally equal, but using connector() is preferred and will be used in almost all examples in this manual.

You can handle connection errors by using the try statement and catch all errors using the errors.Error exception.

import mysql.connector
from mysql.connector import errorcode
try:
  cnx = mysql.connector.connect(user='scott',
                                database='testt')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong your username or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exists")
  else:
    print(err)
else:
  cnx.close()

If you have lots of connection arguments, it's best to keep them in a dictionary and use the **-operator. Here is an example:

import mysql.connector
config = {
  'user': 'scott',
  'password': 'tiger',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**config)
cnx.close()

2.2. Creating Tables Using Connector/Python

All DDL (Data Definition Language) statements are executed using a cursor. The following examples show how to create the tables of the employees database. You will need them for the other examples.

The task is to create the tables of the employee databases.

from __future__ import print_function
import mysql.connector
from mysql.connector import errorcode
DB_NAME = 'employees'
TABLES = {}
TABLES['employees'] = (
    "CREATE TABLE `employees` ("
    "  `emp_no` int(11) NOT NULL AUTO_INCREMENT,"
    "  `birth_date` date NOT NULL,"
    "  `first_name` varchar(14) NOT NULL,"
    "  `last_name` varchar(16) NOT NULL,"
    "  `gender` enum('M','F') NOT NULL,"
    "  `hire_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`)"
    ") ENGINE=InnoDB")
TABLES['departments'] = (
    "CREATE TABLE `departments` ("
    "  `dept_no` char(4) NOT NULL,"
    "  `dept_name` varchar(40) NOT NULL,"
    "  PRIMARY KEY (`dept_no`), UNIQUE KEY `dept_name` (`dept_name`)"
    ") ENGINE=InnoDB")
TABLES['salaries'] = (
    "CREATE TABLE `salaries` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `salary` int(11) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`from_date`), KEY `emp_no` (`emp_no`),"
    "  CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")
TABLES['dept_emp'] = (
    "CREATE TABLE `dept_emp` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `dept_no` char(4) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`dept_no`), KEY `emp_no` (`emp_no`),"
    "  KEY `dept_no` (`dept_no`),"
    "  CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
    "  CONSTRAINT `dept_emp_ibfk_2` FOREIGN KEY (`dept_no`) "
    "     REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")
TABLES['dept_manager'] = (
    "  CREATE TABLE `dept_manager` ("
    "  `dept_no` char(4) NOT NULL,"
    "  `emp_no` int(11) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`dept_no`),"
    "  KEY `emp_no` (`emp_no`),"
    "  KEY `dept_no` (`dept_no`),"
    "  CONSTRAINT `dept_manager_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
    "  CONSTRAINT `dept_manager_ibfk_2` FOREIGN KEY (`dept_no`) "
    "     REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")
TABLES['titles'] = (
    "CREATE TABLE `titles` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `title` varchar(50) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date DEFAULT NULL,"
    "  PRIMARY KEY (`emp_no`,`title`,`from_date`), KEY `emp_no` (`emp_no`),"
    "  CONSTRAINT `titles_ibfk_1` FOREIGN KEY (`emp_no`)"
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")

The above code shows how we are storing the CREATE statements in a Python dictionary called TABLES. We also define the database in a global variable called DB_NAME, which allows you to easily use a different schema.

cnx = mysql.connector.connect(user='scott')
cursor = cnx.cursor()

Note that do not change to a database when connecting to the MySQL server. We want to make sure that the database exists and created when not.

def create_database(cursor):
    try:
        cursor.execute(
            "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
    except mysql.connector.Error as err:
        print("Failed creating database: {}".format(err))
        exit(1)
try:
    cnx.database = DB_NAME    
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_BAD_DB_ERROR:
        create_database(cursor)
        cnx.database = DB_NAME
    else:
        print(err)
        exit(1)

We first try to change to a particular database using the database property of the connection object cnx. If there is an error, we check the error number whether the database is not simply missing. When it is, we call the create_database function to create it for us.

On any other error, the application will exit showing the error message.

for name, ddl in TABLES.iteritems():
    try:
        print("Creating table {}: ".format(name), end='')
        cursor.execute(ddl)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
            print("already exists.")
        else:
            print(err.errmsg)
    else:
        print("OK")
cursor.close()
cnx.close()

After we succesfully created and/or changed to the target database, we create the tables by iterating over the items of the TABLES dictionary.

We handle the error when the table already exists by simply notifying the user that it was already there. Other errors are printed, but we simply continue creating tables.

The output would be something like this:

Creating table employees: already exists.
Creating table salaries: already exists.
Creating table titles: OK
Creating table departments: already exists.
Creating table dept_manager: already exists.
Creating table dept_emp: already exists.

Populating the employees tables can be done using the dump files of the Employee Sample Database. Note that you only need the data dump files that you will find in an archive named like employees_db-dump-files-1.0.5.tar.bz2). If you downloaded the dump files, you can do following from the command line:

shell> tar xzf employees_db-dump-files-1.0.5.tar.bz2
shell> cd employees_db
shell> mysql employees < load_employees.dump
shell> mysql employees < load_titles.dump
shell> mysql employees < load_departments.dump
shell> mysql employees < load_salaries.dump
shell> mysql employees < load_dept_emp.dump
shell> mysql employees < load_dept_manager.dump

2.3. Inserting Data Using Connector/Python

Inserting or updating data is, just like reading data, done using a cursor. Important is that data has to be committed when you are using transactional storage engines (default in MySQL v5.5 and later).

In this example we show how to insert new data. The second INSERT depends on the value of the newly created primary key of the first. We are also demonstrating how to use extended formats. The task is to add a new employee starting to work tomorrow with a salary set to 50000.

Note

The following example uses tables created in the example Section 2.2, “Creating Tables Using Connector/Python”. Especially the AUTO_INCREMENT column option for the primary key of the employees table is important.

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector
cnx = mysql.connector.connect(user='scott', database='employees')
cursor = cnx.cursor()
tomorrow = datetime.now().date() + timedelta(days=1)
add_employee = ("INSERT INTO employees "
               "(first_name, last_name, hire_date, gender, birth_date) "
               "VALUES (%s, %s, %s, %s, %s)")
add_salary = ("INSERT INTO salaries "
              "(emp_no, salary, from_date, to_date) "
              "VALUES (%(emp_no)s, %(salary)s, %(from_date)s, %(to_date)s)")
data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14))
# Insert new employee
cursor.execute(add_employee, data_employee)
emp_no = cursor.lastrowid
# Insert salary information
data_salary = {
  'emp_no': emp_no,
  'salary': 50000,
  'from_date': tomorrow,
  'to_date': date(9999, 1, 1),
}
cursor.execute(add_salary, data_salary)
# Make sure data is commited to the database
cnx.commit()
cursor.close()
cnx.close()

We first open a connection to the MySQL server and store the connection object in the variable cnx. We then create a new cursor, by default a MySQLCursor object, using the connection's cursor() method.

We could calculate tomorrow in the database, but for sake of clarity we do it in Python using the datetime module.

Both insert statements are stored in the variables called add_employee and add_salary. Note that the second INSERT statement is using extended format codes.

The information of the new employee is stored in the tuple data_employee. The query to insert the new employee is executed and we the newly inserted value for the column emp_no using the lastrowid property of the cursor object.

Next we need to insert the new salary for the new employee. We are using the emp_no variable in the directory holding the data. This directoy is then passed to the execute() method of the cursor object.

Since by default Connector/Python turns auto committing off, and MySQL v5.5 and later uses transactional engines by default, it is necessary to commit your changes using the connection's commit() method. You could also rollback using the rollback() method.

2.4. Selecting Data Using Connector/Python

The following example shows how to select data using a cursor created using the connection's cursor() method. The data returned is formatted and printed on the console.

The task is to select all employees hired in the year 1999 and print their names with their hire date to the console.

import datetime
import mysql.connector
cnx = mysql.connector.connect(user='scott', database='employees')
cursor = cnx.cursor()
query = ("SELECT first_name, last_name, hire_date FROM employees "
         "WHERE hire_date BETWEEN %s AND %s")
hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31)
cursor.execute(query, (hire_start, hire_end))
for (first_name, last_name, hire_date) in cursor:
  print("{}, {} was hired on {:%d %b %Y}".format(
    last_name, first_name, hire_date))
cursor.close()
cnx.close()

We first open a connection to the MySQL server and store the connection object in the variable cnx. We then create a new cursor, by default a MySQLCursor object, using the connection's cursor() method.

In the above example we store the SELECT statement in the variable query. Note that we are using unquoted %s-markers where dates should have been. Connector/Python will convert hire_start and hire_end from Python types to something MySQL understand and add quotes. In this case, it will replace the first %s with '1999-01-01', and the second with '1999-12-31'.

We then execute the operation stored in the query query variable using the execute() method. The data used to replace the %s-markers in the query is passed as a tuple: (hire_start, hire_end).

After executing the query, the MySQL server should be ready to send the data. You will need to consume the result set by fetching all rows. There are a few ways for doing this. In this example we use the cursor object as an iterator. The first column in the row will be stored in the variable first_name, the second in last_name and the third in hire_date.

We print then the result, formatting the output using Python's built-in format() function. Note that hire_date was converted automatically by Connector/Python to a Python datetime.date object. This means that we can easily format the date in a more human readable form.

The output should be something like this:

..
Wilharm, LiMin was hired on 16 Dec 1999
Wielonsky, Lalit was hired on 16 Dec 1999
Kamble, Dannz was hired on 18 Dec 1999
DuBourdieux, Zhongwei was hired on 19 Dec 1999
Fujisawa, Rosita was hired on 20 Dec 1999
..

Chapter 3. Connector/Python Connection Arguments

The following lists the arguments which can be used to initiate a connection with the MySQL server using either:

  • Function mysql.connector.connect()
  • Class mysql.connector.MySQLConnection()
Argument NameDefaultDescription
user (username*)  The username used to authenticate with the MySQL Server.
password (passwd*)  The password to authenticate the user with the MySQL Server.
database (db*)  Database name to use when connecting with MySQL.
host127.0.0.1Hostname or IP address of the MySQL Server.
port3306TCP/IP port of the MySQL Server. Must be an integer.
unix_socket The location of the Unix socket file.
use_unicodeTrueWhether to use Unicode or not.
charsetutf8Which MySQL character set to use.
collationutf8_general_ciWhich MySQL collation to use.
autocommitFalseAuto commit transactions.
time_zone Set the time_zone session variable at connection.
sql_mode Set the sql_mode session variable at connection.
get_warningsFalseWhether to fetch warnings.
raise_on_warningsFalseWhether to raise an exception on warnings.
connection_timeout (connect_timeout*) Timeout for the TCP and Unix socket connections.
client_flags MySQL client flags.
bufferedFalseWhether cursor object will fetch result immediately after executing query.
rawFalseWhether MySQL results are returned as-is, that is not converted to Python types.
ssl_ca File containing the SSL certificate authority.
ssl_cert File containing the SSL certificate file.
ssl_key File containing the SSL key.
dsn Not supported (raises NotSupportedError when used).

* Synonymous argument name which is available for compatibility with other Python MySQL drivers. It is encouraged not to use these alternative names.

Authentication with MySQL will use username and password. Note that MySQL Connector/Python does not and will not support the old, insecure passwords of earlier MySQL protocols.

When the database parameter is given, the current database will be set to the given value. You can later change the database using the MySQL USE command, or by setting the database property of the MySQLConnection instance.

By default, Connector/Python tries to connect to a MySQL server running on the localhost using TCP/IP. The host arguments defaults to IP address 127.0.0.1 and port to 3306. Unix sockets are supported by setting unix_socket. Named pipes on the Windows-platform are not supported.

Strings coming from MySQL are by default returned as Python Unicode literals. You can change this behavior by setting use_unicode to False. You can change the character setting for the client connection through the charset argument. If you want to change the character set after connecting to MySQL, set charset-property of the MySQLConnection instance. It is discouraged to use the MySQL SET NAMES SQL command directly. Similar to the charset argument, you can set the collation for the current MySQL session.

Transactions are not automatically committed and the application needs to call the commit()-method of the MySQLConnection instance. It is not recommended to set autocommit to True when using transactional engines.

The time zone can be set per connection using the time_zone argument. This is useful if the MySQL server is set, for example, to UTC and TIMESTAMP values should be returned by MySQL converted to the PST time zone.

MySQL supports so called SQL Modes. which will change the behavior of the server globally or per connection. If you want, for example, warnings raised as errors, you could set sql_mode to TRADITIONAL. For more information, see Server SQL Modes.

Warnings generated by queries will be fetched automatically when get_warnings is set to True. You can also immediately raise an exception by setting raise_on_warnings to True. Consider using Server SQL Modes for turning warnings into errors.

Timeout for connections can be set using connection_timeout.

MySQL uses client flags to enable or disable features. Using the client_flags argument you have control of what is set. To find out what flags are available, you can use the following:

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())

If client_flags is not specified (that is, it is zero), defaults will used for MySQL v4.1 and later. If you specify an integer greater than 0, you need to make sure all flags are set. However, a much better way to set and unset flags is to use a list. For example, want to set FOUND_ROWS, but disable the by default set LONG_FLAG:

flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)

MySQL Connector/Python does not buffer or pre-fetch results. This means the after a query is executed, the programmer is responsible of fetching the data. This is useful for queries returning big result sets. If fetching results is desired, set buffered to True. It is also possible to set this per cursor (see cursor manual).

MySQL types will be converted automatically to Python types. For example, a DATETIME column becomes a datetime.datetime object. When conversion should be done differently to, for example, get better performance, set raw to True.

Using SSL connections is possible when your Python installation supports SSL, that is when it is compiled against the OpenSSL libraries. When you provide the arguments ssl_ca, ssl_key and ssl_cert, the connection will switch to SSL. You can use this in combination with the compressed argument set to True.

passwd, db and connect_timeout are valid for compatibility with other MySQL interfaces and are respectively the same as password, database and connection_timeout. The latter take precedence. Data source name syntax or dsn is not used and will raise a NotSupportedError exception.

Chapter 4. Connector/Python API Reference

Table of Contents

4.1. Errors and Exceptions
4.1.1. Module errorcode
4.1.2. Exception errors.Error
4.1.3. Exception errors.Warning
4.1.4. Exception errors.InterfaceError
4.1.5. Exception errors.DatabaseError
4.1.6. Exception errors.InternalError
4.1.7. Exception errors.OperationalError
4.1.8. Exception errors.ProgrammingError
4.1.9. Exception errors.IntegrityError
4.1.10. Exception errors.DataError
4.1.11. Exception errors.NotSupportedError
4.2. Class connection.MySQLConnection
4.2.1. Constructor connection.MySQLConnection(**kwargs)
4.2.2. Method MySQLConnection.close()
4.2.3. Method MySQLConnection.config(**kwargs)
4.2.4. Method MySQLConnection.connect(**kwargs)
4.2.5. Method MySQLConnection.commit()
4.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)
4.2.7. Method MySQLConnection.cmd_change_user(username='', password='', database='', charset=33)
4.2.8. Method MySQLConnection.cmd_debug()
4.2.9. Method MySQLConnection.cmd_init_db(database)
4.2.10. Method MySQLConnection.cmd_ping()
4.2.11. Method MySQLConnection.cmd_process_info()
4.2.12. Method MySQLConnection.cmd_process_kill(mysql_pid)
4.2.13. Method MySQLConnection.cmd_quit()
4.2.14. Method MySQLConnection.cmd_query(statement)
4.2.15. Method MySQLConnection.cmd_query_iter(statement)
4.2.16. Method MySQLConnection.cmd_refresh(options)
4.2.17. Method MySQLConnection.cmd_shutdown()
4.2.18. Method MySQLConnection.cmd_statistics()
4.2.19. Method MySQLConnection.disconnect()
4.2.20. Method MySQLConnection.get_rows(count=None)
4.2.21. Method MySQLConnection.get_row()
4.2.22. Method MySQLConnection.get_server_info()
4.2.23. Method MySQLConnection.get_server_version()
4.2.24. Method MySQLConnection.is_connected()
4.2.25. Method MySQLConnection.isset_client_flag(flag)
4.2.26. Method MySQLConnection.ping(attempts=1, delay=0)
4.2.27. Method MySQLConnection.reconnect(attempts=1, delay=0)
4.2.28. Method MySQLConnection.rollback()
4.2.29. Method MySQLConnection.set_charset_collation(charset=None, collation=None)
4.2.30. Method MySQLConnection.set_client_flags(flags)
4.2.31. Property MySQLConnection.autocommit
4.2.32. Property MySQLConnection.charset_name
4.2.33. Property MySQLConnection.collation_name
4.2.34. Property MySQLConnection.connection_id
4.2.35. Property MySQLConnection.database
4.2.36. Property MySQLConnection.get_warnings
4.2.37. Property MySQLConnection.raise_on_warnings
4.2.38. Property MySQLConnection.server_host
4.2.39. Property MySQLConnection.server_port
4.2.40. Property MySQLConnection.sql_mode
4.2.41. Property MySQLConnection.time_zone
4.2.42. Property MySQLConnection.unix_socket
4.2.43. Property MySQLConnection.user
4.3. Class cursor.MySQLCursor
4.3.1. Constructor cursor.MySQLCursor
4.3.2. Method MySQLCursor.callproc(procname, args=())
4.3.3. Method MySQLCursor.close()
4.3.4. Method MySQLCursor.execute(operation, params=None, multi=False)
4.3.5. Method MySQLCursor.executemany(operation, seq_params)
4.3.6. Method MySQLCursor.fetchall()
4.3.7. Method MySQLCursor.fetchmany(size=1)
4.3.8. Method MySQLCursor.fetchone()
4.3.9. Method MySQLCursor.fetchwarnings()
4.3.10. Method MySQLCursor.stored_results()
4.3.11. Property MySQLCursor.column_names
4.3.12. Property MySQLCursor.statement
4.3.13. Property MySQLCursor.with_rows
4.4. Class cursor.MySQLCursorBuffered
4.5. Class constants.ClientFlag
4.6. Class constants.FieldType
4.7. Class constants.SQLMode
4.8. Class constants.CharacterSet
4.9. Class constants.RefreshOption

This section contains the public API reference of Connector/Python. Although, valid for both Python v2 and Python v3, examples should be considered working for Python v2.7, and Python v3.1 and greater.

The following overview shows the mysql.connector package with its modules. Currently, only most useful modules, classes and functions for end-users are document.

mysql.connector
  errorcode
  errors
  connection
  constants
  conversion
  cursor
  dbapi
  locales
    eng
      client_error
  protocol
  utils

4.1. Errors and Exceptions

The mysql.connector.errors module defines exception classes for errors and warnings raised by MySQL Connector/Python. Most classes defined in this module are available when you import mysql.connector.

The exception classes defined in this module follow mostly the Python Database Specification v2.0 (PEP-249). For some MySQL client or server errors it is not always clear which exception to raise. It is good to discuss whether an error should be reclassified by opening a bug report.

4.1.1. Module errorcode

This module contains both MySQL server and client error codes defined as module attributes with the error number as value. Using error codes instead of error numbers could make reading the source code a bit easier.

>>> from mysql.connector import errorcode
>>> errorcode.ER_BAD_TABLE_ERROR
1051

See Server Error Codes and Messages and Client Error Codes and Messages.

4.1.2. Exception errors.Error

This exception is the base class for all other exceptions in the errors module. It can be used to catch all errors in a single except statement.

The following example shows how we could catch syntax errors:

import mysql.connector
try:
  cnx = mysql.connector.connect(user='scott', database='employees')
  cursor = cnx.cursor()
  cursor.execute("SELECT * FORM employees")   # Syntax error in query
  cnx.close()
except mysql.connector.Error as err:
  print("Something went wrong: {}".format(err))

Initializing the exception supports a few optional arguments, namely msg, errno, values and sqlstate. All of them are optional and default to None. errors.Error isinternally used by Connector/Python to raise MySQL client and server errors and should not be used by your application to raise exceptions.

The following examples show the result when using no or a combination of the arguments:

>>> from mysql.connector.errors import Error
>>> str(Error())
'Unknown error'
>>> str(Error("Oops! There was an error."))
'Oops! There was an error.'
>>> str(Error(errno=2006))
'2006: MySQL server has gone away'
>>> str(Error(errno=2002, values=('/tmp/mysql.sock', 2)))
"2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"
>>> str(Error(errno=1146, sqlstate='42S02', msg="Table 'test.spam' doesn't exist"))
"1146 (42S02): Table 'test.spam' doesn't exist"

The example which uses error number 1146 is used when Connector/Python receives an error packet from the MySQL Server. The information is parsed and passed to the Error exception as shown.

Each exception subclassing from Error can be initialized using the above mentioned arguments. Additionally, each instance has the attributes errno, msg and sqlstate which can be used in your code.

The following example shows how to handle errors when dropping a table which does not exists (when you do not want to use the IF EXISTS clause):

import mysql.connector
from mysql.connector import errorcode
cnx = mysql.connector.connect(user='scott', database='test')
try:
  cur.execute("DROP TABLE spam")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_BAD_TABLE_ERROR:
    print("Creating table spam")
  else:
    raise

errors.Error is a subclass of the Python StandardError.

4.1.3. Exception errors.Warning

This exception is used for reporting important warnings, however, Connector/Python does not use it. It is included to be compliant with the Python Database Specification v2.0 (PEP-249).

Consider using either more strict Server SQL Modes or the raise_on_warnings connection argument to make Connector/Python raise errors when your queries produce warnings.

errors.Warning is a subclass of the Python StandardError.

4.1.4. Exception errors.InterfaceError

This exception is raised for errors originating from Connector/Python itself, not related to the MySQL server.

errors.InterfaceError is a subclass of errors.Error.

4.1.5. Exception errors.DatabaseError

This exception is the default for any MySQL error which does not fit the other exceptions.

errors.DatabaseError is a subclass of errors.Error.

4.1.6. Exception errors.InternalError

This exception is raised when the MySQL server encounters an internal error, for example, when a deadlock occurred.

errors.InternalError is a subclass of errors.DatabaseError.

4.1.7. Exception errors.OperationalError

This exception is raised for errors which are related to MySQL's operations. For example, to many connections, a hostname could not be resolved, bad handshake, server is shutting down, communication errors, and so on.

errors.OperationalError is a subclass of errors.DatabaseError.

4.1.8. Exception errors.ProgrammingError

This exception is raised on programming errors, for example when you have a syntax error in your SQL or a table was not found.

The following example shows how to handle syntax errors:

try:
    cursor.execute("CREATE DESK t1 (id int, PRIMARY KEY (id))")
except mysql.connector.ProgrammingError as err:
    if err.errno == errorcode.ER_SYNTAX_ERROR:
      print("Check your syntax!")
    else:
      print("Error: {}".format(err))

errors.ProgrammingError is a subclass of errors.DatabaseError.

4.1.9. Exception errors.IntegrityError

This exception is raised when the relational integrity of the data is affected. For example, a duplicate key was inserted or a foreign key constraint would fail.

The following example shows a duplicate key error raised as IntegrityError:

cursor.execute("CREATE TABLE t1 (id int, PRIMARY KEY (id))")
try:
    cursor.execute("INSERT INTO t1 (id) VALUES (1)")
    cursor.execute("INSERT INTO t1 (id) VALUES (1)")
except mysql.connector.IntegrityError as err:
    print("Error: {}".format(err))

errors.IntegrityError is a subclass of errors.DatabaseError.

4.1.10. Exception errors.DataError

This exception is raised when there were problems with the data. Examples are a column set to NULL when it can not, out of range values for a column, division by zero, column count does not match value count, and so on.

errors.DataError is a subclass of errors.DatabaseError.

4.1.11. Exception errors.NotSupportedError

This exception is raised is case some feature was used but not supported by the version of MySQL which returned the error. It is also raised when using functions or statements which are not supported by stored routines.

errors.NotSupportedError is a subclass of errors.DatabaseError.

4.2.  Class connection.MySQLConnection

4.2.1. Constructor connection.MySQLConnection(**kwargs)
4.2.2. Method MySQLConnection.close()
4.2.3. Method MySQLConnection.config(**kwargs)
4.2.4. Method MySQLConnection.connect(**kwargs)
4.2.5. Method MySQLConnection.commit()
4.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)
4.2.7. Method MySQLConnection.cmd_change_user(username='', password='', database='', charset=33)
4.2.8. Method MySQLConnection.cmd_debug()
4.2.9. Method MySQLConnection.cmd_init_db(database)
4.2.10. Method MySQLConnection.cmd_ping()
4.2.11. Method MySQLConnection.cmd_process_info()
4.2.12. Method MySQLConnection.cmd_process_kill(mysql_pid)
4.2.13. Method MySQLConnection.cmd_quit()
4.2.14. Method MySQLConnection.cmd_query(statement)
4.2.15. Method MySQLConnection.cmd_query_iter(statement)
4.2.16. Method MySQLConnection.cmd_refresh(options)
4.2.17. Method MySQLConnection.cmd_shutdown()
4.2.18. Method MySQLConnection.cmd_statistics()
4.2.19. Method MySQLConnection.disconnect()
4.2.20. Method MySQLConnection.get_rows(count=None)
4.2.21. Method MySQLConnection.get_row()
4.2.22. Method MySQLConnection.get_server_info()
4.2.23. Method MySQLConnection.get_server_version()
4.2.24. Method MySQLConnection.is_connected()
4.2.25. Method MySQLConnection.isset_client_flag(flag)
4.2.26. Method MySQLConnection.ping(attempts=1, delay=0)
4.2.27. Method MySQLConnection.reconnect(attempts=1, delay=0)
4.2.28. Method MySQLConnection.rollback()
4.2.29. Method MySQLConnection.set_charset_collation(charset=None, collation=None)
4.2.30. Method MySQLConnection.set_client_flags(flags)
4.2.31. Property MySQLConnection.autocommit
4.2.32. Property MySQLConnection.charset_name
4.2.33. Property MySQLConnection.collation_name
4.2.34. Property MySQLConnection.connection_id
4.2.35. Property MySQLConnection.database
4.2.36. Property MySQLConnection.get_warnings
4.2.37. Property MySQLConnection.raise_on_warnings
4.2.38. Property MySQLConnection.server_host
4.2.39. Property MySQLConnection.server_port
4.2.40. Property MySQLConnection.sql_mode
4.2.41. Property MySQLConnection.time_zone
4.2.42. Property MySQLConnection.unix_socket
4.2.43. Property MySQLConnection.user

The MySQLConnection class is used to open and manage a connection to a MySQL server. It also used to send commands and SQL queries and read result.

4.2.1.  Constructor connection.MySQLConnection(**kwargs)

The MySQLConnection constructor initializes the attributes and when at least one argument is passed, it will try to connect with the MySQL server.

For a complete list or arguments, see Chapter 3, Connector/Python Connection Arguments.

4.2.2. Method MySQLConnection.close()

See disconnect().

Returns a tuple.

4.2.3.  Method MySQLConnection.config(**kwargs)

Allows to configure a MySQLConnection instance after it was instantiated. See Chapter 3, Connector/Python Connection Arguments for a complete list of possible arguments.

You could use the config() method to change, for example, the username and call reconnect().

cnx = MySQLConnection(user='joe', database='test')
# Connected as 'joe'
cnx.config(user='jane')
cnx.reconnect()
# Now connected as 'jane'

4.2.4. Method MySQLConnection.connect(**kwargs)

This method sets up the connection to the MySQL server. If no arguments are given, it will use the already configured or default values. See Chapter 3, Connector/Python Connection Arguments for a complete list of possible arguments.

4.2.5.  Method MySQLConnection.commit()

This method sends the COMMIT command to the MySQL server, committing the current transaction. Since by default, Connector/Python does not auto commit, it is important to call this method after every transaction which updates data for tables using transactional storage engines.

See the rollback() method for rolling back transactions.

>>> cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane'))
>>> cnx.commit()

4.2.6.  Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)

This method returns a MySQLCursor() object, or a subclass of it depending the passed arguments.

When buffered is True, the cursor will fetch all rows after the operation was executed. This is useful when queries return small result setts. Setting raw will skip the conversion from MySQL data types to Python types when fetching rows. Raw is usually used when you want to have more performance and/or you want to do the conversion yourself.

The cursor_class argument can be used to pass a class to use for instantiating a new cursor. It has to be a subclass of cursor.CursorBase.

The returned object depends on the combination of the buffered and raw arguments.

  • if not buffered and not raw: cursor.MySQLCursor
  • if buffered and not raw: cursor.MySQLCursorBuffered
  • if buffered and raw: cursor.MySQLCursorBufferedRaw
  • if not buffered and raw: cursor.MySQLCursorRaw

Returns a CursorBase instance.

4.2.7. Method MySQLConnection.cmd_change_user(username='', password='', database='', charset=33)

Changes the user using username and password. It also causes the specified database to become the default (current) database. It is also possible to change the character set using the charset argument.

Returns a dictionary containing the OK packet information.

4.2.8. Method MySQLConnection.cmd_debug()

Instructs the server to write some debug information to the log. For this to work, the connected user must have the SUPER privilege.

Returns a dictionary containing the OK packet information.

4.2.9. Method MySQLConnection.cmd_init_db(database)

This method makes specified database the default (current) database. In subsequent queries, this database is the default for table references that do not include an explicit database specifier.

Returns a dictionary containing the OK packet information.

4.2.10. Method MySQLConnection.cmd_ping()

Checks whether the connection to the server is working.

This method is not to be used directly. Use ping() or is_connected() instead.

Returns a dictionary containing the OK packet information.

4.2.11. Method MySQLConnection.cmd_process_info()

This method will raise the NotSupportedError exception. Instead, use the SHOW PROCESSLIST statement or query the tables found in the database INFORMATION_SCHEMA.

4.2.12. Method MySQLConnection.cmd_process_kill(mysql_pid)

Asks the server to kill the thread specified by mysql_pid. Although still available, it's better to use the SQL KILL command.

Returns a dictionary containing the OK packet information.

The following two lines do the same:

>>> cnx.cmd_process_kill(123)
>>> cnx.cmd_query('KILL 123')

4.2.13. Method MySQLConnection.cmd_quit()

This method sends the QUIT command to the MySQL server, closing the current connection. Since there is no response from the MySQL, the packet that was sent is returned.

4.2.14. Method MySQLConnection.cmd_query(statement)

This method sends the given statement to the MySQL server and returns a result. If you need to send multiple statements, you have to use the cmd_query_iter() method.

The returned dictionary contains information depending on what kind of query was executed. If the query is a SELECT statement, the result will contain information about columns. Other statements will return a dictionary containing OK or EOF packet information.

Errors received from the MySQL server will be raised as exceptions. An InterfaceError is raised when multiple results are found.

Returns a dictionary.

4.2.15. Method MySQLConnection.cmd_query_iter(statement)

Similar to the cmd_query() method, but returns a generator object to iterate through results. cmd_query_iter() should be used when sending multiple statements which are separated by semicolons.

The following example shows how to iterate through the results after sending multiple statements:

statement = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cnx.cmd_query(statement, iterate=True):
  if 'columns' in result:
      columns = result['columns']
      rows = cnx.get_rows()
  else:
      # do something useful with INSERT result

Returns a generator object.

4.2.16. Method MySQLConnection.cmd_refresh(options)

This method flushes tables or caches, or resets replication server information. The connected user must have the RELOAD privilege.

The options argument should be a bitwise value using constants from the class constants.RefreshOption.

See Section 4.9, “ Class constants.RefreshOption for a list of options.

Example:

>>> from mysql.connector import RefreshOption
>>> refresh = RefreshOption.LOG | RefreshOption.THREADS
>>> cnx.cmd_refresh(refresh)

4.2.17. Method MySQLConnection.cmd_shutdown()

Asks the database server to shut down. The connected user must have the SHUTDOWN privilege.

Returns a dictionary containing the OK packet information.

4.2.18. Method MySQLConnection.cmd_statistics()

Returns a dictionary containing information about the MySQL server including uptime in seconds and the number of running threads, questions, reloads, and open tables.

4.2.19. Method MySQLConnection.disconnect()

This method will try to send the QUIT command and close the socket. It does not raise any exceptions.

MySQLConnection.close() is a synonymous method name and more commonly used.

4.2.20. Method MySQLConnection.get_rows(count=None)

This method retrieves all or remaining rows of a query result set, returning a tuple containing the rows as sequence and the EOF packet information. The count argument can be used to get a given amount of rows. If count is not specified or None, all rows are retrieved.

The tuple returned by get_rows() consists of:

  • a list of tuples containing the row data as byte objects or an empty list when no rows are available.

  • EOF packet information as dictionary containing status_flag and warning_count.

An InterfaceError is raised when all rows have been retrieved.

The get_rows() method is used by MySQLCursor to fetch rows.

Returns a tuple.

4.2.21. Method MySQLConnection.get_row()

This method retrieves the next row of a query result set, returning tuple.

The tuple returned by get_row() consists of:

  • the row as a tuple containing byte objects or None when no more rows are available.

  • EOF packet information as dictionary containing status_flag and warning_count or None when the row returned is not the last row.

The get_row() method is used by MySQLCursor to fetch rows.

4.2.22.  Method MySQLConnection.get_server_info()

This method returns the MySQL server information verbatim, for example '5.5.24-log', or None when not connected.

Returns a string or None.

4.2.23.  Method MySQLConnection.get_server_version()

This method returns the MySQL server version as a tuple or None when not connected.

Returns a tuple or None.

4.2.24.  Method MySQLConnection.is_connected()

Reports whether the connection to MySQL Server is available.

This method checks whether the connection to MySQL is available using the ping() method, but unlike ping(), is_connected() returns True when the connection is available, False otherwise.

Returns True or False.

4.2.25.  Method MySQLConnection.isset_client_flag(flag)

This method returns True if the client flag was set, false otherwise.

Returns True or False.

4.2.26.  Method MySQLConnection.ping(attempts=1, delay=0)

Check whether the connection to the MySQL server is still available.

When reconnect is set to True, one or more attempts are made to try to reconnect to the MySQL server using the reconnect() method. Use the delay argument (seconds) if you want to wait between each retry.

When the connection is not available, an InterfaceError is raised. Use the is_connected() method if you just want to check the connection without raising an error.

Raises InterfaceError on errors.

4.2.27.  Method MySQLConnection.reconnect(attempts=1, delay=0)

Attempt to reconnect with the MySQL server.

The argument attempts should be the number of times a reconnect is tried. The delay argument is the number of seconds to wait between each retry.

You may want to set the number of attempts higher and use a longer delay when you expect the MySQL server to be down for maintenance or when you expect the network to be temporary unavailable.

4.2.28.  Method MySQLConnection.rollback()

This method sends the ROLLBACK command to the MySQL server, committing the current transaction. Since by default, Connector/Python does not auto commit, it is possible to cancel transactions when using transactional storage engines.

See the commit() method for committing transactions.

>>> cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane'))
>>> cnx.rollback()

4.2.29.  Method MySQLConnection.set_charset_collation(charset=None, collation=None)

This method sets the character set and collation to be used for the current connection. The charset argument can be either the name of a character set, or the numerical equivalent as defined in constants.CharacterSet.

When collation is None, the default will be looked up and used.

The charset argument then be either:

In the following example, we set the character set to latin1 and the collation will be set to the default latin1_swedish_ci:

>>> cnx = mysql.connector.connect(user='scott')
>>> cnx.set_charset('latin1')

Specify a specific collation as follows:

>>> cnx = mysql.connector.connect(user='scott')
>>> cnx.set_charset('latin1', 'latin1_general_ci')

4.2.30.  Method MySQLConnection.set_client_flags(flags)

This method sets the client flags which are used when connecting with the MySQL server and returns the new value. The flags argument can be either an integer or a sequence of valid client flag values (see Section 4.5, “ Class constants.ClientFlag).

If flags is a sequence, each item in the sequence will set the flag when the value is positive or unset it when negative. For example, to unset LONG_FLAG and set the FOUND_ROWS flags:

>>> from mysql.connector.constants import ClientFlag
>>> cnx.set_client_flags([ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG])
>>> cnx.reconnect()

Note that client flags are only set or used when connecting with the MySQL server. It is therefor necessary to reconnect after making changes.

Returns an integer.

4.2.31.  Property MySQLConnection.autocommit

This property is used to toggle the auto commit feature of MySQL and retrieve the current state. When the value evaluates to True, auto commit will be turned, otherwise it is turned off.

Note that auto commit is disabled by default when connecting through Connector/Python. This can be toggled using the connection parameter autocommit.

When the auto commit is turned off, you have to commit transactions when using transactional storage engines such as InnoDB or NDBCluster.

>>> cnx.autocommit
False
>>> cnx.autocommit = True
>>> cnx.autocommit
True

Returns True or False.

4.2.32.  Property MySQLConnection.charset_name

This property returns which character set is used for the connection whether it is connected or not.

Returns a string.

4.2.33.  Property MySQLConnection.collation_name

This property returns which collation is used for the connection whether it is connected or not.

Returns a string.

4.2.34.  Property MySQLConnection.connection_id

This property returns the connection ID (thread ID or session ID) for the current connection or None when not connected.

Returns a integer or None.

4.2.35.  Property MySQLConnection.database

This property is used to set current (active) database executing the USE command. The property can also be used to retrieve the current database name.

>>> cnx.database = 'test'
>>> cnx.database = 'mysql'
>>> cnx.database
u'mysql'

Returns a string.

4.2.36.  Property MySQLConnection.get_warnings

This property is used to toggle whether warnings should be fetched automatically or not. It accepts True or False (default).

Fetching warnings automatically could be useful when debugging queries. Cursors will make warnings available through the method MySQLCursor.fetchwarnings().

>>> cnx.get_warnings = True
>>> cursor.execute('SELECT "a"+1')
>>> cursor.fetchall()
[(1.0,)]
>>> cursor.fetchwarnings()
[(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'")]

Returns True or False.

4.2.37.  Property MySQLConnection.raise_on_warnings

This property is used to toggle whether warnings should raise exceptions or not. It accepts True or False (default).

Toggling raise_on_warnings will also toggle get_warnings since warnings need to be fetched so they can be raised as exceptions.

Note that you might always want to check setting SQL Mode if you would like to have the MySQL server directly report warnings as errors. It is also good to use transactional engines so transactions can be rolled back when catching the exception.

Result sets needs to be fetched completely before any exception can be raised. The following example shows the execution of a query which produces a warning

>>> cnx.raise_on_warnings = True
>>> cursor.execute('SELECT "a"+1')
>>> cursor.fetchall()
..
mysql.connector.errors.DataError: 1292: Truncated incorrect DOUBLE value: 'a'

Returns True or False.

4.2.38.  Property MySQLConnection.server_host

This read-only property returns the hostname or IP address used for connecting with the MySQL server.

Returns a string.

4.2.39.  Property MySQLConnection.server_port

This read-only property returns the TCP/IP port used for connecting with the MySQL server.

Returns a integer.

4.2.40.  Property MySQLConnection.sql_mode

This property is used to retrieve and set the SQL Modes for the current. The value should be list of different modes separated by comma (","), or a sequence of modes, preferably using the constants.SQLMode class.

To unset all modes, pass an empty string or an empty sequence.

>>> cnx.sql_mode = 'TRADITIONAL,NO_ENGINE_SUBSTITUTION'
>>> cnx.sql_mode.split(',')
[u'STRICT_TRANS_TABLES', u'STRICT_ALL_TABLES', u'NO_ZERO_IN_DATE',
u'NO_ZERO_DATE', u'ERROR_FOR_DIVISION_BY_ZERO', u'TRADITIONAL', 
u'NO_AUTO_CREATE_USER', u'NO_ENGINE_SUBSTITUTION']
>>> from mysql.connector.constants import SQLMode
>>> cnx.sql_mode = [ SQLMode.NO_ZERO_DATE, SQLMode.REAL_AS_FLOAT]
>>> cnx.sql_mode
u'REAL_AS_FLOAT,NO_ZERO_DATE'

Returns a string.

4.2.41.  Property MySQLConnection.time_zone

This property is used to set the time zone session variable for the current connection and retrieve it.

>>> cnx.time_zone = '+00:00'
>>> cur.execute('SELECT NOW()') ; cur.fetchone()
(datetime.datetime(2012, 6, 15, 11, 24, 36),)
>>> cnx.time_zone = '-09:00'
>>> cur.execute('SELECT NOW()') ; cur.fetchone()
(datetime.datetime(2012, 6, 15, 2, 24, 44),)
>>> cnx.time_zone
u'-09:00'

Returns a string.

4.2.42.  Property MySQLConnection.unix_socket

This read-only property returns the UNIX socket user for connecting with the MySQL server.

Returns a string.

4.2.43.  Property MySQLConnection.user

This read-only property returns the username used for connecting with the MySQL server.

Returns a string.

4.3.  Class cursor.MySQLCursor

The MySQLCursor class is used to instantiate object which can execute operation such as SQL queries. They interact with the MySQL server using a MySQLConnection object.

4.3.1.  Constructor cursor.MySQLCursor

The constructor initializes the instance with the optional connection, which should be an instance of MySQLConnection.

In most cases, the MySQLConnection method cursor() is used to instantiate a MySQLCursor object.

4.3.2.  Method MySQLCursor.callproc(procname, args=())

This method calls a stored procedure with the given name. The args sequence of parameters must contain one entry for each argument that the routine expects. The result is returned as modified copy of the input sequence. Input parameters are left untouched, output and input/output parameters replaced with possibly new values.

Result set provided by the stored procedure are automatically fetched and stored as MySQLBufferedCursor instances. See stored_results() for more information.

The following example shows how to execute a stored procedure which takes two parameters, multiplies the values and returns the product:

# Definition of the multiply stored procedure:
# CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
# BEGIN
#  SET pProd := pFac1 * pFac2;
# END
>>> args = (5,5,0) # 0 is to hold pprod
>>> cursor.callproc('multiply', args)
>>> cursor.fetchone())
('5', '5', 25L)

4.3.3.  Method MySQLCursor.close()

This method will close the MySQL cursor, resetting all results and removing the connection.

Use close() every time you are done using the cursor.

4.3.4.  Method MySQLCursor.execute(operation, params=None, multi=False)

This method prepare the given database operation (query or command). The parameters found in the tuple or dictionary params will be bound to the variables in the operation. Variables are specified using %s markers or named markers %(name)s.

For example, insert information about a new employee and selecting again the data of this person:

insert = (
"INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
"VALUES (%s, %s, %s, %s)")
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
cursor.execute(insert, data)
select = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"
cursor.execute(select, { 'emp_no': 2 })

Note that the data is converted from Python object to something MySQL understand. In the above example, the datetime.date() instance is converted to '2012-03-23' in the above example.

When multi is set to True, execute() will be able to execute multiple statements. It will return an iterator which makes it possible to go through all results for each statement. Note that using parameters is not working well in this case, and it's usually a good idea to execute each statement on its own.

In the following example we select and insert data in one operation and display the result:

operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
for result in cursor.execute(operation):
if result.with_rows:
  print("Statement '{}' has following rows:".format(
    result.statement))
  print(result.fetchall())
else:
  print("Affected row(s) by query '{}' was {}".format(
        result.statement, result.rowcount))

If the connection was configured to fetch warnings, warnings generated by the operation will be available through the method MySQLCursor.fetchwarnings().

Returns an iterator when multi is True.

4.3.5.  Method MySQLCursor.executemany(operation, seq_params)

This method prepares a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_params.

The executemany() is simply iterating through the sequence of parameters calling the execute() method. Inserting data, however, is optimized by batching them using the multiple rows syntax.

In the following example we are inserting 3 records:

data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)

In the above example, the INSERT statement sent to MySQL would be as follows: INSERT INTO employees (first_name, hire_date) VALUES ('Jane', '2005-02-12'), ('Joe', '2006-05-23'), ('John', '2010-10-03').

Note that it is not possible to execute multiple statements using the executemany() method. Doing so will raise an InternalError exception.

4.3.6.  Method MySQLCursor.fetchall()

The method fetches all or remaining rows of a query result set, returning a list of tuples. An empty list is returned when no rows are (anymore) available.

The following examples shows how to retrieve the first 2 rows of a result set, and then retrieve the remaining rows:

>>> cursor.execute("SELECT * FROM employees ORDER BY emp_no")
>>> head_rows = cursor.fetchmany(size=2)
>>> remaining_rows = cursor.fetchall()

Note that you have to fetch all rows before being able to execute new queries using the same connection.

Returns a list of tuples or empty list when no rows available.

4.3.7.  Method MySQLCursor.fetchmany(size=1)

This method fetches the next set of rows of a query results, returning a list of tuples. An empty list is returned when no more rows are available.

The number of rows returned can be specified using the size argument, which defaults to one. Fewer rows might be returned, when there are not more rows available than specified by the argument.

Note that you have to fetch all rows before being able to execute new queries using the same connection.

Returns a list of tuples or empty list when no rows available.

4.3.8.  Method MySQLCursor.fetchone()

This method retrieves the next row of a query result set, returning a single sequence, or None when no more data is available.The returned tuple consists of data returned by the MySQL server converted to Python objects.

The fetchone() method is used by fetchmany() and fetchall(). It is also used when using the MySQLCursor instance as an iterator.

The following examples show how to iterate through the result of a query using fetchone():

# Using a while-loop
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
while row is not None:
print(row)
row = cursor.fetchone()
# Using the cursor as iterator 
cursor.execute("SELECT * FROM employees")
for row in cursor:
print(row)

Note that you have to fetch all rows before being able to execute new queries using the same connection.

Returns a tuple or None.

4.3.9.  Method MySQLCursor.fetchwarnings()

This method returns a list of tuples containing warnings generated by previously executed statement. Use the connection's get_warnings property to toggle whether warnings has to be fetched.

The following example shows a SELECT statement which generated a warning:

>>> cnx.get_warnings = True
>>> cursor.execute('SELECT "a"+1')
>>> cursor.fetchall()
[(1.0,)]
>>> cursor.fetchwarnings()
[(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'")]

It is also possible to raise errors when warnings are found. See the MySQLConnection property raise_on_warnings.

Returns a list of tuples.

4.3.10.  Method MySQLCursor.stored_results()

This method returns an list iterator object which can be used to go through result sets provided by stored procedures after calling them using the callproc() method.

In the following example we execute a stored procedure which will provide two result sets. We use stored_results() to retrieve them:

>>> cursor.callproc('sp1')
()
>>> for result in cursor.stored_results():
...     print result.fetchall()
... 
[(1,)]
[(2,)]

Note that the result sets stay available until you executed another operation or call another stored procedure.

Returns a listiterator.

4.3.11.  Property MySQLCursor.column_names

This read-only property returns the column names of a result set as sequence of (unicode) strings.

The following example shows how you can create a dictionary out of a tuple containing data with keys using column_names:

cursor.execute("SELECT last_name, first_name, hire_date "
             "FROM employees WHERE emp_no = %s", (123,))
row = dict(zip(cursor.column_names, cursor.fetchone())
print("{last_name}, {first_name}: {hire_date}".format(row))

Returns a tuple.

4.3.12.  Property MySQLCursor.statement

This read-only property returns the last executed statement. In case multiple statements where executed, it will show the actual statement.

The statement property might be useful for debugging and showing what was send to the MySQL server.

Returns a string.

4.3.13.  Property MySQLCursor.with_rows

This read-only property will return True when the result of the executed operation provides rows.

The with_rows property is useful when executing multiple statements and you need to fetch rows. In the following example we only report the affected rows by the UPDATE statement:

import mysql.connector
cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()
operation = 'SELECT 1; UPDATE t1 SET c1 = 2; SELECT 2'
for result in cursor.execute(operation, multi=True):
  if result.with_rows:
    result.fetchall()
  else:
    print("Updated row(s): {}".format(result.rowcount))

4.4.  Class cursor.MySQLCursorBuffered

This class is inheriting from cursor.MySQLCursor and if needed automatically retrieves rows after an operation has been executed.

MySQLCursorBuffered can be useful in situations where two queries, with small result sets, need to be combined or computed with each other.

You can either use the buffered argument when using the connection's cursor() method, or you can use the buffered connection option to make all created cursors by default buffering.

import mysql.connector
cnx = mysql.connector.connect()
# Only this particular cursor will be buffering results
cursor.cursor(buffered=True)
# All cursors by default buffering
cnx = mysql.connector.connect(buffered=True)

See Section 1.1, “Tutorial: Raise employee's salary using a buffering cursor” for a practical use case.

4.5.  Class constants.ClientFlag

This class provides constants defining MySQL client flags which can be used upon connection to configure the session. The ClientFlag class is available when importing mysql.connector.

>>> import mysql.connector
>>> mysql.connector.ClientFlag.FOUND_ROWS
2

See Section 4.2.30, “ Method MySQLConnection.set_client_flags(flags) and the connection argument client_flag.

Note that the ClientFlag class can not be instantiated.

4.6.  Class constants.FieldType

This class provides all supported MySQL field or data types. They can be useful when dealing with raw data or defining your own converters. The field type is stored with every cursor in the description for each column.

The following example shows how you can print the name of the data types for each of the columns in the result set.

from __future__ import print_function
import mysql.connector
from mysql.connector import FieldType
cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()
cursor.execute(
  "SELECT DATE(NOW()) AS `c1`, TIME(NOW()) AS `c2`, "
  "NOW() AS `c3`, 'a string' AS `c4`, 42 AS `c5`")
rows = cursor.fetchall()
for desc in cursor.description:
  colname = desc[0]
  coltype = desc[1]
    print("Column {} has type {}".format(
      colname, FieldType.get_info(coltype)))
cursor.close()
cnx.close()

Note that the FieldType class can not be instantiated.

4.7.  Class constants.SQLMode

This class provides all known MySQL Server SQL Modes. It is mostly used when setting the SQL modes at connection time using the connection's property sql_mode. See Section 4.2.40, “ Property MySQLConnection.sql_mode.

Note that the SQLMode class can not be instantiated.

4.8.  Class constants.CharacterSet

This class provides all known MySQL characters sets and their default collations. See Section 4.2.29, “ Method MySQLConnection.set_charset_collation(charset=None, collation=None) for examples.

Note that the CharacterSet class can not be instantiated.

4.9.  Class constants.RefreshOption

  • RefreshOption.GRANT

    Refresh the grant tables, like FLUSH PRIVILEGES.

  • RefreshOption.LOG

    Flush the logs, like FLUSH LOGS.

  • RefreshOption.TABLES

    Flush the table cache, like FLUSH TABLES.

  • RefreshOption.HOSTS

    Flush the host cache, like FLUSH HOSTS.

  • RefreshOption.STATUS

    Reset status variables, like FLUSH STATUS.

  • RefreshOption.THREADS

    Flush the thread cache.

  • RefreshOption.SLAVE

    On a slave replication server, reset the master server information and restart the slave, like RESET SLAVE.

  • RefreshOption.MASTER

    On a master replication server, remove the binary log files listed in the binary log index and truncate the index file, like RESET MASTER.