Chapter 8 The Connector/Python C Extension

Table of Contents

8.1 Application Development with the Connector/Python C Extension
8.2 The _mysql_connector C Extension Module

Connector/Python supports (as of version 2.1.1) a C Extension that interfaces with the MySQL C client library. For queries that return large result sets, using the C Extension can improve performance compared to a pure Python implementation of the MySQL client/server protocol. Section 8.1, “Application Development with the Connector/Python C Extension”, describes how applications that use the mysql.connector module can use the C Extension. It is also possible to use the C Extension directly, by importing the _mysql_connector module rather than the mysql.connector module. See Section 8.2, “The _mysql_connector C Extension Module”. For information about installing the C Extension, see Chapter 4, Connector/Python Installation.

8.1 Application Development with the Connector/Python C Extension

Installations of Connector/Python from version 2.1.1 on support a use_pure argument to connect() that indicates whether to use the pure Python interface to MySQL or the C Extension that uses the MySQL C client library:

  • By default, use_pure is True and the C Extension is not used even for Connector/Python installations that include it.

  • For Connector/Python installations that include the C Extension, enable it by passing a use_pure=False argument to connect().

  • For Connector/Python installations that do not include the C Extension, passing use_pure=False to connect() raises an exception.

  • For older Connector/Python installations that know nothing of the C Extension (before version 2.1.1), passing use_pure to connect() raises an exception regardless of its value.

Note

On OS X, if your Connector/Python installation includes the C Extension, but Python scripts are unable to use it, try setting your DYLD_LIBRARY_PATH environment variable the directory containing the C client library. For example:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib   (for sh)
setenv DYLD_LIBRARY_PATH /usr/local/mysql/lib   (for tcsh)

If you built the C Extension from source, this directory should be the one containing the C client library against which the extension was built.

If you need to check whether your Connector/Python installation is aware of the C Extension, test the HAVE_CEXT value. There are different approaches for this. Suppose that your usual arguments for connect() are specified in a dictionary:

config = {
  'user': 'scott',
  'password': 'tiger',
  'host': '127.0.0.1',
  'database': 'employees',
}

The following example illustrates one way to add use_pure to the connection arguments:

import mysql.connector

if mysql.connector.__version_info__ > (2, 1) and mysql.connector.HAVE_CEXT:
  config['use_pure'] = False

Alternatively, add use_pure to the configuration arguments as follows:

try:
  have_cext = mysql.connector.HAVE_CEXT
except AttributeError:
  have_cext = False

if have_cext:
  config['use_pure'] = False

8.2 The _mysql_connector C Extension Module

To use the C Extension directly, import the _mysql_connector module rather than mysql.connector, then use the _mysql_connector.MySQL() class to obtain a MySQL instance. For example:

import _mysql_connector

ccnx = _mysql_connector.MySQL()
ccnx.connect(user='scott', password='tiger',
             host='127.0.0.1', database='employees')

ccnx.query("SHOW VARIABLES LIKE 'version%'")
row = ccnx.fetch_row()
while row:
  print(row)
  row = ccnx.fetch_row()
ccnx.free_result()

ccnx.close()

For more information, see Chapter 11, Connector/Python C Extension API Reference.