MySQL Connectivity With Python
MySQL Connectivity With Python
( Page 2 of 7 )
In the words of its author, MySQLdb is "a thread-compatible interface to the popular
MySQL database server that provides the Python database API." Developed by
Andy Dustman, this module is needed for MySQL database connectivity under Python.
The first thing to do is make sure that you have the MySQLdb module installed on your
Python development system. The easiest way to check this via the interactive Python
command line interpreter - as the following example demonstrates:
If you see something like the error message above, you can safely assume that the module
is not installed on your system. Drop by http://sourceforge.net/projects/mysql-python,
download the latest copy of the MySQLdb distribution, and extract and install it on your
system.
$ cd MySQL-python-0.9.2
Now, you can verify that the module has been installed by trying to import it again via the
command line.
With that out of the way, here's a simple example that demonstrates some of the functionality of
the DBI. Consider the following database table,
and then consider this short Python script, which connects to the database and prints out the data
within the table.
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
Most of this is self-explanatory, but let me go through it with you briefly anyway.
The first step is to import the MySQLdb module, via Python's "import" function.
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
A successful connection returns a Connection object, which you can use to create a cursor.
# create a cursor
cursor = db.cursor()
This cursor is needed to execute an SQL statement, and to retrieve the generated resultset.
A number of methods are available to retrieve the SQL resultset - the one used here is the
fetchall() method, which returns a tuple of tuples, each inner tuple representing a row of the
resultset. This tuple can then be iterated over with a regular "for" loop, and its elements printed
to the standard output.
You can also use the fetchone() method of the cursor object to get and display rows one at a
time, instead of all at once. Consider the following example, which demonstrates how this might
work:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
In this case, the rowcount() method of the cursor object is first used to find out the number of
rows in the resultset. This number is then used in a "for" loop to iterate over the resultset, with
the fetchone() method used to move forward through the resultset and sequentially display the
contents of each row.
You can get MySQLdb to give you a specific subset of the resultset with the cursor object's
fetchmany() method, which allows you to specify the size of the returned resultset. Consider the
following example, which demonstrates:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
# execute SQL statement
cursor.execute("SELECT * FROM animals")
Obviously, you can also perform INSERT, UPDATE and DELETE queries via the MySQLdb
module. Consider the following example, which illustrates:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
You can modify this so that the values for the query string are input by the user - take a look at
this variant of the example above, which demonstrates:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
This time, when you run the script, you'll be asked for the values to be inserted into the database.
Notice the manner in which variables have been integrated into the SQL query in the example
above. The %s placeholder is used to represent each variable in the query string, with the actual
values stored in a tuple and passed as second argument.
In case you have auto-increment fields in your database, you can use the cursor object's
insert_id() method to obtain the ID of the last inserted record - this comes in handy when you're
dealing with linked tables in an RDBMS, as newly-inserted IDs from one table often serve as
keys into other tables. The following code snippet should demonstrate how this works:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
Many database scripts involve preparing a single query (an INSERT, for example) and then
executing it again and again with different values. MySQLdb comes with an executemany()
method, which simplifies this task and can also reduce performance overhead.
In order to understand how this works, consider the following example, which demonstrates:
#!/usr/bin/python
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
In this case, the same query is repeated multiple times, with a different set of values each time.
The values for each iteration are provided to the executemany() method as a Python list; each
element of the list is a tuple containing the values for that iteration.
Using this technique, it's possible to write a script that asks the user to enter a series of data
values, and then inserts them all into the database in one swell foop using the executemany()
method. Which is just what I've done below:
#!/usr/bin/python
if name == "EOF":
break
# connect
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",
db="db56a")
# create a cursor
cursor = db.cursor()
In this case, a "while" loop is used to continuously throw up user prompts, with each set of
values entered by the user being packaged into a tuple and added to the "data" list. Once the user
completes entering all the data, an executemany() statement is used, in combination with the
various input values, to INSERT the values into the database.
A number of other methods come bundled with the MySQLdb class - here's a brief list of the more
interesting ones:
And that's about all for the moment. In this article, I showed you how to configure and install the Python
MySQLdb module, and use it to hook your Python scripts up to a MySQL database. I demonstrated the
different techniques available for iterating over a resultset, showed you the basics of using variable
placeholders and prepared queries, and illustrated some of the ancillary methods available in the
module.
While this tutorial should get you and running with Python and MySQL, you shouldn't stop reading right
away. Here are a few links worth checking out:
Note: All examples in this article have been tested on Linux/i586 with Python 1.5.2, MySQL 3.23 and
MySQLdb 0.9.2. Examples are illustrative only, and are not meant for a production environment.
Melonfire provides no warranties or support for the source code described in this article. YMMV!