15.4. Testing the Python Interface to MySQL

We use the following simple MySQL program to prove that the Python MySQL interface is up and running.

CD-ROM reference=15003.txt
C>type testmysql.py

"""
Program to test Python interface to MySQL
"""
import MySQL

# Connect to MySQL.
db=MySQL.connect('','')

# Select the mysql database. This database is
# always available in a MySQL system.
db.selectdb("mysql")

# Retrieve a list of tables in that database.
query = db.query("show tables")

# Retrieve the rows that make up the query result.
rows=query.fetchrows()

# Print them out.
print rows

The result of executing the program is shown below.

CD-ROM reference=15004.txt
C>python testmysql.py
[['db'], ['host'], ['user']]

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.