# pydoc > MySQLdb

---
type: CommandReference
command: MySQLdb
mode: pydoc
section: 
source: pydoc3
---

## Quick Reference

- `import MySQLdb` — Import the package
- `MySQLdb.connect(host, user, passwd, db)` — Create a connection to a MySQL database
- `MySQLdb.escape_string(s)` — Escape special SQL characters in a string
- `MySQLdb.string_literal(obj)` — Convert object to a SQL string literal (escaped and quoted)
- `MySQLdb.get_client_info()` — Get MySQL client library version string
- `MySQLdb.DateFromTicks(ticks)` — Convert UNIX timestamp to a `date` object
- `MySQLdb.TimeFromTicks(ticks)` — Convert UNIX timestamp to a `time` object
- `MySQLdb.TimestampFromTicks(ticks)` — Convert UNIX timestamp to a `datetime` object

## Name

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

## Description

This package is a wrapper around `_mysql`, which mostly implements the MySQL C API. For information on how MySQLdb handles type conversion, see the `MySQLdb.converters` module.

## Package Contents

- `_exceptions` — Exception classes
- `_mysql` — Low-level MySQL C API wrapper
- `compat` — Compatibility helpers
- `connections` — Connection class
- `constants` — Constants package
- `converters` — Type conversion
- `cursors` — Cursor class
- `release` — Release information
- `times` — Time-related helpers

## Submodules

- `FIELD_TYPE` — Field type constants

## Classes

### Exception Hierarchy

- `builtins.Exception` -> `MySQLdb._exceptions.MySQLError`
  - `MySQLdb._exceptions.Error` (base for all errors except Warning)
    - `MySQLdb._exceptions.DatabaseError` (errors related to the database)
      - `MySQLdb._exceptions.DataError` — problems with processed data (e.g., division by zero, out of range)
      - `MySQLdb._exceptions.IntegrityError` — relational integrity violations (e.g., foreign key, duplicate key)
      - `MySQLdb._exceptions.InternalError` — internal database errors (e.g., invalid cursor, transaction sync)
      - `MySQLdb._exceptions.NotSupportedError` — unsupported database API usage (e.g., rollback on non-transactional connection)
      - `MySQLdb._exceptions.OperationalError` — operational errors (e.g., disconnect, data source not found)
      - `MySQLdb._exceptions.ProgrammingError` — programming errors (e.g., table not found, SQL syntax error)
    - `MySQLdb._exceptions.InterfaceError` — errors related to the database interface, not the database itself
  - `MySQLdb._exceptions.Warning` (inherits from `builtins.Warning` as well) — important warnings (e.g., data truncation)

### `MySQLdb._exceptions.MySQLError`

- Exception related to operation with MySQL. Base for all MySQLdb exceptions.

### `MySQLdb._exceptions.Error`

- Base class of all other error exceptions (not Warning).

### `MySQLdb._exceptions.DatabaseError`

- Exception raised for errors that are related to the database.

### `MySQLdb._exceptions.DataError`

- Exception raised for errors due to problems with the processed data (e.g., division by zero, numeric value out of range).

### `MySQLdb._exceptions.IntegrityError`

- Exception raised when the relational integrity of the database is affected (e.g., foreign key check fails, duplicate key).

### `MySQLdb._exceptions.InternalError`

- Exception raised when the database encounters an internal error (e.g., cursor not valid, transaction out of sync).

### `MySQLdb._exceptions.NotSupportedError`

- Exception raised in case a method or database API was used which is not supported by the database (e.g., requesting rollback on a non-transactional connection).

### `MySQLdb._exceptions.OperationalError`

- Exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer (e.g., unexpected disconnect, data source name not found, transaction could not be processed, memory allocation error).

### `MySQLdb._exceptions.ProgrammingError`

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

### `MySQLdb._exceptions.InterfaceError`

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

### `MySQLdb._exceptions.Warning`

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

### `DBAPISet`

- A special type of set for which `A == x` is true if `A` is a `DBAPISet` and `x` is a member of that set. Inherits from `frozenset`. Used for type constants.

### `Date` (datetime.date)

- Standard Python `datetime.date` class. Used for date type conversion. Provides methods: `ctime()`, `isocalendar()`, `isoformat()`, `isoweekday()`, `replace()`, `strftime()`, `timetuple()`, `toordinal()`, `weekday()`, and class/static methods: `fromisocalendar()`, `fromisoformat()`, `fromordinal()`, `fromtimestamp()`, `today()`.

### `Time` (datetime.time)

- Standard Python `datetime.time` class. Used for time type conversion. Provides methods: `dst()`, `isoformat()`, `replace()`, `strftime()`, `tzname()`, `utcoffset()`, and class method `fromisoformat()`.

### `Timestamp` (datetime.datetime)

- Standard Python `datetime.datetime` class. Used for datetime type conversion. Provides methods: `astimezone()`, `ctime()`, `date()`, `dst()`, `isoformat()`, `replace()`, `time()`, `timestamp()`, `timetuple()`, `timetz()`, `tzname()`, `utcoffset()`, `utctimetuple()`, and class methods: `combine()`, `fromisoformat()`, `fromtimestamp()`, `now()`, `strptime()`, `utcfromtimestamp()`, `utcnow()`.

## Functions

- `Binary(x)` — (no additional description)
- `Connect(*args, **kwargs)` — Factory function for `connections.Connection`. Creates a new database connection.
- `Connection` — Alias for `Connect`.
- `DateFromTicks(ticks)` — Convert UNIX ticks into a `date` instance.
- `TimeFromTicks(ticks)` — Convert UNIX ticks into a `time` instance.
- `TimestampFromTicks(ticks)` — Convert UNIX ticks into a `datetime` instance.
- `connect` — Alias for `Connect`.
- `debug(s)` — Does a `DBUG_PUSH` with the given string. Uses the Fred Fish debug library. Requires client library compiled with debugging support.
- `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.
- `escape_string(s)` — Quote any SQL-interpreted characters in string `s`. Use `connection.escape_string(s)` instead; `_mysql.escape_string(s)` cannot handle character sets.
- `get_client_info()` — Returns a string that represents the client library version.
- `string_literal(obj)` — Converts object `obj` into a SQL string literal. Escapes special characters and encloses in single quotes: `"'%s'" % escape_string(str(obj))`. Use `connection.string_literal(obj)` instead; `_mysql.string_literal(obj)` cannot handle character sets.

## Data

- `BINARY` — `DBAPISet({249, 250, 251, 252})`
- `DATE` — `DBAPISet({10})`
- `NUMBER` — `DBAPISet({0, 1, 3, 4, 5, 8, 9, 13, 246})`
- `ROWID` — `DBAPISet()`
- `STRING` — `DBAPISet({253, 254, 247})`
- `TIME` — `DBAPISet({11})`
- `TIMESTAMP` — `DBAPISet({12, 7})`
- `__all__` — List of exported names: `['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date', ...]`
- `apilevel` — `'2.0'`
- `paramstyle` — `'format'`
- `threadsafety` — `1`
- `version_info` — `(1, 4, 6, 'final', 0)`

## Version

1.4.6

## Author

Inada Naoki <<songofacandy@gmail.com>>