MySQLdb Package — MySQLdb 1.2.4b4 documentation

# 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.__init__.` `Binary`(x)

`MySQLdb.__init__.` `Connect`(*args, **kwargs)

  Factory function for connections.Connection.

`MySQLdb.__init__.` `Connection`(*args, **kwargs)

  Factory function for connections.Connection.

`MySQLdb.__init__.` `Date`

  alias of `date`

`MySQLdb.__init__.` `Time`

  alias of `time`

`MySQLdb.__init__.` `Timestamp`

  alias of `datetime`

`MySQLdb.__init__.` `DateFromTicks`(ticks)

  Convert UNIX ticks into a date instance.

`MySQLdb.__init__.` `TimeFromTicks`(ticks)

  Convert UNIX ticks into a time instance.

`MySQLdb.__init__.` `TimestampFromTicks`(ticks)

  Convert UNIX ticks into a datetime instance.

exception `MySQLdb.__init__.` `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.__init__.` `DatabaseError`

  Bases: `_mysql_exceptions.Error`

  Exception raised for errors that are related to the database.

exception `MySQLdb.__init__.` `Error`

  Bases: `_mysql_exceptions.MySQLError`

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

exception `MySQLdb.__init__.` `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.__init__.` `InterfaceError`

  Bases: `_mysql_exceptions.Error`

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

exception `MySQLdb.__init__.` `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.__init__.` `MySQLError`

  Bases: `exceptions.StandardError`

  Exception related to operation with MySQL.

exception `MySQLdb.__init__.` `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.__init__.` `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.__init__.` `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.__init__.` `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.__init__.` `Warning`

  Bases: `exceptions.Warning`, `_mysql_exceptions.MySQLError`

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

`MySQLdb.__init__.` `connect`(*args, **kwargs)

  Factory function for connections.Connection.

`MySQLdb.__init__.` `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.__init__.` `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.__init__.` `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.__init__.` `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.__init__.` `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.__init__.` `get_client_info`()

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

`MySQLdb.__init__.` `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 `Connection.` `DatabaseError`

    Bases: `_mysql_exceptions.Error`

    Exception raised for errors that are related to the database.

  exception `Connection.` `Error`

    Bases: `_mysql_exceptions.MySQLError`

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

  exception `Connection.` `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 `Connection.` `InterfaceError`

    Bases: `_mysql_exceptions.Error`

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

  exception `Connection.` `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 `Connection.` `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 `Connection.` `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 `Connection.` `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 `Connection.` `Warning`

    Bases: `exceptions.Warning`, `_mysql_exceptions.MySQLError`

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

  `Connection.` `autocommit`(on)
  `Connection.` `begin`()

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

  `Connection.` `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.

  `Connection.` `default_cursor`

    alias of `Cursor`

  `Connection.` `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.

  `Connection.` `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.

  `Connection.` `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.

  `Connection.` `set_sql_mode`(sql_mode)

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

  `Connection.` `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.

## `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.` `Instance2Str`(o, d)

  Convert an Instance to a string representation. If the __str__() method produces acceptable output, then you don’t need to add the class to conversions; it will be handled by the default converter. If the exact class is not found in d, it will use the first class it can find for which o is an instance.

`MySQLdb.converters.` `Long2Int`(s, d)

  Convert something into a string via str().

`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.

## Subpackages

- constants Package
  - `constants` Package
  - `CLIENT` Module
  - `CR` Module
  - `ER` Module
  - `FIELD_TYPE` Module
  - `FLAG` Module
  - `REFRESH` Module