Saturday, June 14, 2008

Python mysql access

Here is how you can access mysql using python:
#!/usr/bin/python

import MySQLdb
import sys
import time

db = MySQLdb.connect(host="reportsdb",user="root", db="tool_metrics")
dbc = db.cursor()
str = "select '1','2','3'";
dbc.execute(str)
records = dbc.fetchall()

for r in records:
print r[0]+","+r[1]+","+r[2]

For more information on Python and databases I recommend the following book for futher reading: Python Pocket Reference (Pocket Reference (O'Reilly))

No comments: