MySQLdb Package

MySQLdb Package

MySQLdb - A DB API v2.0 compatible interface to MySQL.

This package is a wrapper around _mysql, which mostly implements the MySQL C API.

connect() – connects to server

See the C API specification and the MySQL documentation for more info on other items.

For information on how MySQLdb handles type conversion, see the MySQLdb.converters module.

MySQLdb.Binary(x)
MySQLdb.Connect(*args, **kwargs)

Factory function for connections.Connection.

MySQLdb.Connection(*args, **kwargs)

Factory function for connections.Connection.

MySQLdb.Date

alias of date

MySQLdb.Time

alias of time

MySQLdb.Timestamp

alias of datetime

MySQLdb.DateFromTicks(ticks)

Convert UNIX ticks into a date instance.

MySQLdb.TimeFromTicks(ticks)

Convert UNIX ticks into a time instance.

MySQLdb.TimestampFromTicks(ticks)

Convert UNIX ticks into a datetime instance.

exception MySQLdb.DataError

Bases: _mysql_exceptions.DatabaseError

Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc.

exception MySQLdb.DatabaseError

Bases: _mysql_exceptions.Error

Exception raised for errors that are related to the database.

exception MySQLdb.Error

Bases: _mysql_exceptions.MySQLError

Exception that is the base class of all other error exceptions (not Warning).

exception MySQLdb.IntegrityError

Bases: _mysql_exceptions.DatabaseError

Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails, duplicate key, etc.

exception MySQLdb.InterfaceError

Bases: _mysql_exceptions.Error

Exception raised for errors that are related to the database interface rather than the database itself.

exception MySQLdb.InternalError

Bases: _mysql_exceptions.DatabaseError

Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc.

exception MySQLdb.MySQLError

Bases: Exception

Exception related to operation with MySQL.

exception MySQLdb.NotSupportedError

Bases: _mysql_exceptions.DatabaseError

Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off.

class MySQLdb.DBAPISet

Bases: frozenset

A special type of set for which A == x is true if A is a DBAPISet and x is a member of that set.

exception MySQLdb.OperationalError

Bases: _mysql_exceptions.DatabaseError

Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

exception MySQLdb.ProgrammingError

Bases: _mysql_exceptions.DatabaseError

Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc.

exception MySQLdb.Warning

Bases: Warning, _mysql_exceptions.MySQLError

Exception raised for important warnings like data truncations while inserting, etc.

MySQLdb.connect(*args, **kwargs)

Factory function for connections.Connection.

MySQLdb.debug()

Does a DBUG_PUSH with the given string. mysql_debug() uses the Fred Fish debug library. To use this function, you must compile the client library to support debugging.

MySQLdb.escape()

escape(obj, dict) – escape any special characters in object obj using mapping dict to provide quoting functions for each type. Returns a SQL literal string.

MySQLdb.escape_dict()

escape_sequence(d, dict) – escape any special characters in dictionary d using mapping dict to provide quoting functions for each type. Returns a dictionary of escaped items.

MySQLdb.escape_sequence()

escape_sequence(seq, dict) – escape any special characters in sequence seq using mapping dict to provide quoting functions for each type. Returns a tuple of escaped items.

MySQLdb.escape_string()

escape_string(s) – quote any SQL-interpreted characters in string s.

Use connection.escape_string(s), if you use it at all. _mysql.escape_string(s) cannot handle character sets. You are probably better off using connection.escape(o) instead, since it will escape entire sequences as well as strings.

MySQLdb.get_client_info()

get_client_info() – Returns a string that represents the client library version.

MySQLdb.string_literal()

string_literal(obj) – converts object obj into a SQL string literal. This means, any special SQL characters are escaped, and it is enclosed within single quotes. In other words, it performs:

“’%s’” % escape_string(str(obj))

Use connection.string_literal(obj), if you use it at all. _mysql.string_literal(obj) cannot handle character sets.

connections Module

This module implements connections for MySQLdb. Presently there is only one class: Connection. Others are unlikely. However, you might want to make your own subclasses. In most cases, you will probably override Connection.default_cursor with a non-standard Cursor class.

class MySQLdb.connections.Connection(*args, **kwargs)

Bases: _mysql.connection

MySQL Database Connection Object

exception DataError

Bases: _mysql_exceptions.DatabaseError

Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc.

exception DatabaseError

Bases: _mysql_exceptions.Error

Exception raised for errors that are related to the database.

exception Error

Bases: _mysql_exceptions.MySQLError

Exception that is the base class of all other error exceptions (not Warning).

exception IntegrityError

Bases: _mysql_exceptions.DatabaseError

Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails, duplicate key, etc.

exception InterfaceError

Bases: _mysql_exceptions.Error

Exception raised for errors that are related to the database interface rather than the database itself.

exception InternalError

Bases: _mysql_exceptions.DatabaseError

Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc.

exception NotSupportedError

Bases: _mysql_exceptions.DatabaseError

Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off.

exception OperationalError

Bases: _mysql_exceptions.DatabaseError

Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

exception ProgrammingError

Bases: _mysql_exceptions.DatabaseError

Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc.

exception Warning

Bases: Warning, _mysql_exceptions.MySQLError

Exception raised for important warnings like data truncations while inserting, etc.

autocommit(on)
begin()

Explicitly begin a connection. Non-standard. DEPRECATED: Will be removed in 1.3. Use an SQL BEGIN statement instead.

cursor(cursorclass=None)

Create a cursor on which queries may be performed. The optional cursorclass parameter is used to create the Cursor. By default, self.cursorclass=cursors.Cursor is used.

default_cursor

alias of Cursor

errorhandler(connection, cursor, errorclass, errorvalue)

If cursor is not None, (errorclass, errorvalue) is appended to cursor.messages; otherwise it is appended to connection.messages. Then errorclass is raised with errorvalue as the value.

You can override this with your own error handler by assigning it to the instance.

literal(o)

If o is a single object, returns an SQL literal as a string. If o is a non-string sequence, the items of the sequence are converted and returned as a sequence.

Non-standard. For internal use; do not use this in your applications.

query(query)
set_character_set(charset)

Set the connection character set to charset. The character set can only be changed in MySQL-4.1 and newer. If you try to change the character set from the current value in an older version, NotSupportedError will be raised.

set_sql_mode(sql_mode)

Set the connection sql_mode. See MySQL documentation for legal values.

show_warnings()

Return detailed information about warnings as a sequence of tuples of (Level, Code, Message). This is only supported in MySQL-4.1 and up. If your server is an earlier version, an empty sequence is returned.

waiter = None

converters Module

MySQLdb type conversion module

This module handles all the type conversions for MySQL. If the default type conversions aren’t what you need, you can make your own. The dictionary conversions maps some kind of type to a conversion function which returns the corresponding value:

Key: FIELD_TYPE.* (from MySQLdb.constants)

Conversion function:

Arguments: string

Returns: Python object

Key: Python type object (from types) or class

Conversion function:

Arguments: Python object of indicated type or class AND
conversion dictionary

Returns: SQL literal value

Notes: Most conversion functions can ignore the dictionary, but
it is a required parameter. It is necessary for converting things like sequences and instances.

Don’t modify conversions if you can avoid it. Instead, make copies (with the copy() method), modify the copies, and then pass them to MySQL.connect().

MySQLdb.converters.Bool2Str(s, d)
MySQLdb.converters.Float2Str(o, d)
MySQLdb.converters.None2NULL(o, d)

Convert None to NULL.

MySQLdb.converters.Set2Str(s, d)
MySQLdb.converters.Str2Set(s)
MySQLdb.converters.Thing2Literal(o, d)

Convert something into a SQL string literal. If using MySQL-3.23 or newer, string_literal() is a method of the _mysql.MYSQL object, and this function will be overridden with that method when the connection is created.

MySQLdb.converters.Thing2Str(s, d)

Convert something into a string via str().

MySQLdb.converters.Unicode2Str(s, d)

Convert a unicode object to a string using the default encoding. This is only used as a placeholder for the real function, which is connection-dependent.

MySQLdb.converters.array2Str(o, d)
MySQLdb.converters.char_array(s)
MySQLdb.converters.quote_tuple(t, d)

cursors Module

MySQLdb Cursors

This module implements Cursors of various types for MySQLdb. By default, MySQLdb uses the Cursor class.

class MySQLdb.cursors.Cursor(connection)

Bases: MySQLdb.cursors.CursorStoreResultMixIn, MySQLdb.cursors.CursorTupleRowsMixIn, MySQLdb.cursors.BaseCursor

This is the standard Cursor class that returns rows as tuples and stores the result set in the client.

times Module

times module

This module provides some Date and Time classes for dealing with MySQL data.

Use Python datetime module to handle date and time columns.

MySQLdb.times.DateFromTicks(ticks)

Convert UNIX ticks into a date instance.

MySQLdb.times.DateTime2literal(d, c)

Format a DateTime object as an ISO timestamp.

MySQLdb.times.DateTimeDelta2literal(d, c)

Format a DateTimeDelta object as a time.

MySQLdb.times.DateTime_or_None(s)
MySQLdb.times.Date_or_None(s)
MySQLdb.times.TimeDelta_or_None(s)
MySQLdb.times.TimeFromTicks(ticks)

Convert UNIX ticks into a time instance.

MySQLdb.times.Time_or_None(s)
MySQLdb.times.TimestampFromTicks(ticks)

Convert UNIX ticks into a datetime instance.

MySQLdb.times.format_TIMEDELTA(v)
MySQLdb.times.format_TIMESTAMP(d)
MySQLdb.times.mysql_timestamp_converter(s)

Convert a MySQL TIMESTAMP to a Timestamp object.