DB-API supports database-independent database access. The MySQL implementation of this API, MySQLdb, can be downloaded from http://dustman.net/andy/python/MySQLdb. It comes with a RedHat RPM Linux installer, a Win32 installer, and a Python script for other platforms. For those other platforms:
Uncompress the .tar.gz file that contains MySQLdb using the commands gunzip FILENAME.tar.gz and tar xf FILENAME.tar.
Change directories into the newly generated MySQLdb directory.
Issue the command: python setup.py install.
The MySQLdb module contains the standard DB-API methods and attributes as well as several proprietary methods and attributes. Proprietary APIs are marked with asterisks.
The entry point into the MySQL module is via the MySQLdb.connect( ) method. The return value from this method represents a connection to a MySQL database that you can use for all of your MySQL operations.
paramstyle |
Defines the type of parameter placeholder in parameterized queries. DB-API supports many valid values for this attribute, but MySQLdb actually supports only format and pyformat. This attribute is largely meaningless to MySQL developers.
threadsafety |
Specifies the level of thread safety supported by MySQLdb. Possible values are:
type_conv |
Maps MySQL types from strings to the desired mapping type. This value is initialized with:
{ FIELD_TYPE.TINY : int, FIELD_TYPE.SHORT: int, FIELD_TYPE.LONG: long, FIELD_TYPE.FLOAT: float, FIELD_TYPE.DOUBLE: float, FIELD_TYPE.LONGLONG: long, FIELD_TYPE.INT24: int, FIELD_TYPE.YEAR: int }
MySQL.connect() |
connection = MySQL.connect(params)
Connects to the MySQL database engine represented by the various connection keyword/value parameters. These parameters include:
This method returns a Python object representing a connection to a MySQL database.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test');
close() |
close( )
Closes the current connection to the database and releases any associated resources.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); connection.close( );
commit() |
commit( )
Commits the current transaction by sending a COMMIT to MySQL.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); connection._transactional = 1; cursor = connection.cursor( ); cursor.execute("UPDATE TNAME SET COL = 1 WHERE PK = 2045"); cursor.execute("UPDATE TNAME SET COL = 1 WHERE PK = 3200"); connection.commit( ); connection.close( );
cursor() |
cursor = cursor( )
Creates a cursor associated with this connection. Transactions involving any statements executed by the newly created cursor are governed by this connection.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute("UPDATE TNAME SET COL = 1 WHERE PK = 2045"); connection.close( );
rollback( ) |
rollback( )
Rolls back any uncommitted statements. This works only if MySQL is set up for transactional processing in this context.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); connection._transactional = 1; cursor = connection.cursor( ); cursor.execute("UPDATE TNAME SET COL = 1 WHERE PK = 2045"); try: cursor.execute("UPDATE TNAME SET COL = 1 WHERE PK = 3200"); connection.commit( ); except: connection.rollback( ); connection.close( );
arraysize |
Specifies the number of rows to fetch at a time with the fetchmany( ) method call. By default, this value is set to 1. In other words, fetchmany( ) fetches one row at a time by default.
description |
Describes a result column as a read-only sequence of seven-item sequences. Each sequence contains the following values: name, type_code, display_size, internal_size, precision, scale, and null_ok.
callproc( ) |
callproc(procname [,parameters])
This method is not supported by MySQL.
Method: close( ) |
close( )
Closes the cursor explicitly. Once closed, a cursor will throw a ProgrammingError if any operation is attempted on the cursor.
cursor = connection.cursor( ); cursor.close( );
execute( ) |
cursor = execute(sql [,parameters])
Sends arbitrary SQL to MySQL for execution. If the SQL specified is parameterized, the optional second argument is a sequence or mapping containing parameter values for the SQL. Any results or other information generated by the SQL can then be accessed through the cursor.
The parameters of this method may also be lists of tuples to enable you to perform multiple operations at once. This usage is considered deprecated as of the DB-API 2.0 specification. You should use the executemany( ) method instead.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute('SELECT * FROM TNAME');
executemany( ) |
cursor.executemany(sql,parameters)
Prepares an SQL statement and sends it to MySQL for execution against all parameter sequences or mappings in the parameters sequence.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.executemany("INSERT INTO COLOR ( COLOR, ABBREV ) VALUES (%s, %s )", (("BLUE", "BL"), ("PURPLE", "PPL"), ("ORANGE", "ORN")));
Method: fetchall( ) |
rows = cursor.fetchall( )
Fetches all remaining rows of a query result as a sequence of sequences.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute("SELECT * FROM TNAME"); for row in cursor.fetchall( ): # process row
fetchmany( ) |
rows = cursor.fetchmany([size])
Fetches the next set of rows of a result set as a sequence of sequences. If no more rows are available, this method returns an empty sequence.
If specified, the size parameter dictates how many rows should be fetched. The default value for this parameter is the cursor's arraysize value. If the size parameter is larger than the number of rows left, the resulting sequence will contain all remaining rows.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute("SELECT * FROM TNAME"); rows = cursor.fetchmany(5);
fetchone( ) |
row = cursor.fetchone( )
Fetches the next row of a result set returned by a query as a single sequence. This method will return None when no more results exist. It will throw an error if the SQL executed is not a query.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute("SELECT * FROM TNAME"); row = cursor.fetchone( ); print "Key: ", row[0]; print "Value: ", row[1];
insert_id( )* |
id = cursor.insert_id( )
Returns the last inserted ID from the most recent INSERT on an AUTO_INCREMENT field.
connection = MySQLdb.connect(host='carthage', user='test', passwd='test', db='test'); cursor = connection.cursor( ); cursor.execute("INSERT INTO TNAME (COL) VALUES (1)"); id = cursor.insert_id( );
Copyright © 2003 O'Reilly & Associates. All rights reserved.