MySQL Connector/Python - MySQL drive written in Python
Bases: mysql.connector.mysql.MySQLBase
Class implementing Python DB API v2.0.
Shortcut for executing COMMIT.
a new MySQLConnection object through the __init__ method.
Possible parameters are:
database db
Initial database to use once we are connected with the MySQL Server. The db argument is synonym, but database takes precedence.
>>> from mysql.connector.constants import ClientFlag
>>> print '
‘.join(ClientFlag.get_full_info())
- buffered
- When set to True .execute() will fetch the rows immediatly.
Shortcut for executing ROLLBACK
Shortcut for creating a mysql.MySQL object.
Bases: mysql.connector.constants._constants
Bases: mysql.connector.constants._constantflags
Field flags as found in MySQL sources mysql-src/include/mysql_com.h
Bases: mysql.connector.constants._constantflags
Client Options as found in the MySQL sources mysql-src/include/mysql_com.h
Bases: mysql.connector.constants._constants
List of supported character sets with their collations. This maps to the character set we get from the server within the handshake packet.
This list is hardcoded because we want to avoid doing each time the above query to get the name of the character set used.
Returns information about the charset and optional collation.
Returns info string about the charset for given MySQL ID.
Returns information about the charset for given MySQL ID.
Returns a list with names of all supproted character sets.
Bases: mysql.connector.constants._constants
Options used when sending the COM_REFRESH server command.
Bases: exceptions.StandardError
Bases: exceptions.StandardError
Bases: mysql.connector.errors.Error
Bases: mysql.connector.errors.Error
Shortcut for creating a mysql.MySQL object.
alias of date
alias of time
alias of datetime
alias of str
Holds version of MySQL Connector/Python
Implementing communication to MySQL servers
Bases: object
Base class for MySQL Connections subclasses.
Should not be used directly but overloaded, changing the open_connection part. Examples over subclasses are
MySQLTCPConnection MySQLUNIXConnection
Receive packets using the socket from the server.
Send packets using the socket to the server.
Bases: mysql.connector.connection.MySQLBaseConnection
Opens a TCP connection to the MySQL Server.
Opens a TCP Connection and checks the MySQL handshake.
Bases: mysql.connector.connection.MySQLBaseConnection
Opens a connection through the UNIX socket of the MySQL Server.
Opens a UNIX socket and checks the MySQL handshake.
Various MySQL constants and character sets
Bases: mysql.connector.constants._constants
List of supported character sets with their collations. This maps to the character set we get from the server within the handshake packet.
This list is hardcoded because we want to avoid doing each time the above query to get the name of the character set used.
Returns information about the charset and optional collation.
Returns info string about the charset for given MySQL ID.
Returns information about the charset for given MySQL ID.
Returns a list with names of all supproted character sets.
Bases: mysql.connector.constants._constantflags
Client Options as found in the MySQL sources mysql-src/include/mysql_com.h
Bases: mysql.connector.constants._constantflags
Field flags as found in MySQL sources mysql-src/include/mysql_com.h
Bases: mysql.connector.constants._constants
Bases: mysql.connector.constants._constants
Options used when sending the COM_REFRESH server command.
Bases: mysql.connector.constants._constants
Bases: mysql.connector.constants._constantflags
Server flags as found in the MySQL sources mysql-src/include/mysql_com.h
Converting MySQL and Python types
Bases: object
Bases: mysql.connector.conversion.ConverterBase
o escape method: for escpaing values send to MySQL o quoting method: for quoting values send to MySQL in statements o conversion mapping: maps Python and MySQL data types to
function for converting them.
This class should be overloaded whenever one needs differences in how values are to be converted. Each MySQLConnection object has a default_converter property, which can be set like
MySQL.converter(CustomMySQLConverter)
Escapes special characters as they are expected to by when MySQL receives them. As found in MySQL source mysys/charset.c
Returns the value if not a string, or the escaped string.
Returns a string.
Converts a given value coming from MySQL to a certain type in Python. The flddsc contains additional information for the field in the table. It’s an element from MySQLCursor.description.
Returns a mixed value.
Cursor classes
Bases: object
Base for defining MySQLCursor. This class is a skeleton and defines methods and members as required for the Python Database API Specification v2.0.
It’s better to inherite from MySQLCursor.
Bases: mysql.connector.cursor.CursorBase
Default cursor which fetches all rows and stores it for later usage. It uses the converter set for the MySQLConnection to map MySQL types to Python types automatically.
This class should be inherited whenever other functionallity is required. An example would to change the fetch* member functions to return dictionaries instead of lists of values.
Implements the Python Database API Specification v2.0.
Possible parameters are:
Calls a stored procedue with the given arguments
The arguments will be set during this session, meaning they will be called like _<procname>__arg<nr> where <nr> is an enumeration (+1) of the arguments.
1) Definining the Stored Routine in MySQL: CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT) BEGIN
SET pProd := pFac1 * pFac2;
END
2) Executing in Python: args = (5,5,0) # 0 is to hold pprod cursor.callproc(multiply, args) print cursor.fetchone()
The last print should output (‘5’, ‘5’, 25L)
Does not return a value, but a result set will be available when the CALL-statement execute succesfully. Raises exceptions when something is wrong.
Close the cursor, disconnecting it from the MySQL object.
Returns True when succesful, otherwise False.
Executes the given operation. The parameters given through params are used to substitute %%s in the operation string. For example, getting all rows where id is 5:
cursor.execute(“SELECT * FROM t1 WHERE id = %s”, (5,))
If warnings where generated, and db.get_warnings is True, then self._warnings will be a list containing these warnings.
Raises exceptions when any error happens.
Loops over seq_params and calls excute()
Used for iterating over the result set. Calles self.fetchone() to get the next row.
Bases: mysql.connector.cursor.MySQLCursor
Cursor which fetches rows within execute()
DB API v2.0 required
Python exceptions
Bases: object
Bases: mysql.connector.errors.Error
Bases: exceptions.StandardError
Bases: mysql.connector.errors.Error
Bases: exceptions.StandardError
Main classes for interacting with MySQL
Bases: mysql.connector.mysql.MySQLBase
Class implementing Python DB API v2.0.
Shortcut for executing COMMIT.
a new MySQLConnection object through the __init__ method.
Possible parameters are:
database db
Initial database to use once we are connected with the MySQL Server. The db argument is synonym, but database takes precedence.
>>> from mysql.connector.constants import ClientFlag
>>> print '
‘.join(ClientFlag.get_full_info())
- buffered
- When set to True .execute() will fetch the rows immediatly.
Shortcut for executing ROLLBACK
Bases: object
MySQLBase
To be implemented while subclassing MySQLBase.
Disconnect from the MySQL server.
Returns the server version as a string
Returns the MySQL threadid of the connection.
Returns the server version as a tuple
Check whether we are connected to the MySQL server.
Check whether we are connected to the MySQL server.
Set auto commit on or off. The argument ‘switch’ must be a boolean type.
Sets whether cursor .execute() fetches rows
Set the character set used for the connection. This is the recommended way of change it per connection basis. It does execute SET NAMES internally, but it’s good not to use this command directly, since we are setting some other members accordingly.
Set the converter class to be used. This should be a class overloading methods and members of conversion.MySQLConverter.
Set the database to be used after connection succeeded.
Set the host for connection to the MySQL server.
Set the username and/or password for the user connecting to the MySQL Server.
Set the TCP port to be used when connecting to the server, usually 3306.
Set whether we return string fields as unicode or not. Default is True.
Set the UNIX Socket location. Does not check if it exists.
Set how to handle warnings coming from MySQL
Set wheter we should get warnings whenever an operation produced some. If you set raise_on_warnings to True, any warning will be raised as a DataError exception.
Implementing the MySQL Client/Server protocol
Bases: mysql.connector.protocol.PacketOut
Bases: mysql.connector.protocol.Auth
Bases: mysql.connector.protocol.PacketOut
Bases: mysql.connector.protocol.PacketIn
Returns a description as a list useful for cursors.
This function returns a list as defined in the Python Db API v2.0 specification.
Bases: mysql.connector.protocol.PacketIn
Bases: mysql.connector.protocol.CommandPacket
Bases: object
Class handling the MySQL Protocol.
MySQL v4.1 Client/Server Protocol is currently supported.
Change the user with given username and password to another optional database.
Send DEBUG command to the MySQL Server
Needs SUPER privileges. The output will go to the MySQL server error log.
Returns True when it was succesful.
Send command to server to change databases.
Ping the MySQL server to check if the connection is still alive.
Returns True when alive, False when server doesn’t respond.
Gets the process list from the MySQL Server.
Returns a list of dictionaries which corresponds to the output of SHOW PROCESSLIST of MySQL. The data is converted to Python types.
Kills a MySQL process using it’s ID.
The mypid must be an integer.
Sends a query to the server.
Returns a tuple, when the query returns a result. The tuple consist number of fields and a list containing their descriptions. If the query doesn’t return a result set, the an OKResultPacket will be returned.
Closes the current connection with the server.
Send the Refresh command to the MySQL server.
The argument should be a bitwise value using the protocol.RefreshOption constants.
Usage:
RefreshOption = mysql.connector.RefreshOption refresh = RefreshOption.LOG | RefreshOption.THREADS db.cmd_refresh(refresh)
Shuts down the MySQL Server.
Careful with this command if you have SUPER privileges! (Which your scripts probably don’t need!)
Returns True if it succeeds.
Sends statistics command to the MySQL Server
Returns a dictionary with various statistical information.
Make and send the authentication using information found in the handshake packet.
Check whether the buffer is a valid handshake. If it is, we set some member variables for later usage. The handshake packet is returned for later usuage, e.g. authentication.
Takes a buffer and readers information from header.
Returns a tuple (pktsize, pktnr)
Check if the given buffer is a MySQL EOF Packet. It should start with þ and be smaller 9 bytes.
Returns boolean.
Check if the given buffer is a MySQL Error Packet.
Buffer should start with ÿ.
Returns boolean.
Check if the given buffer is a MySQL OK Packet. It should start with .
Returns boolean.
Get data for 1 row
Get one row’s data. Should be called after getting the field descriptions.
Returns a tuple with 2 elements: a row’s data and the EOF packet.
Get all rows
Returns a tuple with 2 elements: a list with all rows and the EOF packet.
Gather data from the given handshake.
Bases: mysql.connector.protocol.BasePacket
Each packet type used in the MySQL Client Protocol is build on the Packet class. It defines lots of useful functions for parsing and sending data to and from the MySQL Server.
Utilities
Takes a bytes and returns it was an integer.
Returns integer.
Takes an unsigned byte (1 byte) and packs it as string.
Returns string.
Takes a string of 2 bytes and unpacks it as unsigned integer.
Returns integer.
Takes an unsigned short (2 bytes) and packs it as string.
Returns string.
Takes a string of 3 bytes and unpacks it as integer.
Returns integer.
Takes an unsigned integer (3 bytes) and packs it as string.
Returns string.
Takes a string of 4 bytes and unpacks it as integer.
Returns integer.
Takes an unsigned integer (4 bytes) and packs it as string.
Returns string.
Takes a string of 8 bytes and unpacks it as integer.
Returns integer.
Takes a string and unpacks it as an integer.
This function uses int1read, int2read, int3read and int4read by checking the length of the given string.
Returns integer.
Takes an unsigned integers and packs it as a string.
This function uses int1store, int2store, int3store and int4store depending on the integer value.
returns string.
Reads bytes from a buffer.
Returns a tuple with buffer less the read bytes, and the bytes.
Take a buffer and reads an integer of a certain size (1 <= size <= 4).
Returns a tuple (truncated buffer, int)
Takes a buffer and reads an length code string from the start.
Returns a tuple with buffer less the integer and the integer read.
Takes a buffer and reads a length coded string from the start.
This is how Length coded strings work
If the string is 250 bytes long or smaller, then it looks like this:
<– 1b –> +———-+————————- | length | a string goes here +———-+————————-
If the string is bigger than 250, then it looks like this:
<- 1b -><- 2/3/4 -> +——+———–+————————- | type | length | a string goes here +——+———–+————————-
- if type == ü:
- length is code in next 2 bytes
- elif type == ý:
- length is code in next 3 bytes
- elif type == þ:
- length is code in next 4 bytes
NULL has a special value. If the buffer starts with û then it’s a NULL and we return None as value.
Returns a tuple (trucated buffer, string).
Reads all length encoded strings from the given buffer.
This is exact same function as read_lc_string() but duplicated in hopes for performance gain when reading results.
Reads a string up until a character or for a given size.
Returns a tuple (trucated buffer, string).