Sunday, October 17, 2010

mysql client with python

1. Install MySQL for Python

a. install mysql and mysql-devel pkg

[root@skcc-nebdap37 src]# yum install mysql mysql-devel


b. install python setuptools from http://pypi.python.org/pypi/setuptools#files


[root@skcc-nebdap37 src]# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e

[root@skcc-nebdap37 src]# gunzip setuptools-0.6c11.tar.gz

[root@skcc-nebdap37 src]# tar xvf setuptools-0.6c11.tar

[root@skcc-nebdap37 src]# cd setuptools-0.6c11

[root@skcc-nebdap37 src]# python setup.py build

[root@skcc-nebdap37 src]# python setup.py install


c. Download and install MySQL for Python from http://sourceforge.net/projects/mysql-python


2. Sample MySQL for Python code


#!/usr/bin/python

import MySQLdb

# Establich a connection
db = MySQLdb.connection(host="localhost",
user="nebula",
passwd="nebula",
db="nebula")

# Run a MySQL query from Python and get the result set

db.query("""select * from USERS""")
r = db.store_result()

# Iterate through the result set
# Example calls back up to 100 rows
for row in r.fetch_row(100):
print row

No comments: