In order to connect to Mysql from Python, I used MySQLdb library. It is an interface to the MySQL database server for Python. For more information - pypi.python.org/pypi/MySQL-python.
I installed MySQL-python in my environment following the steps below.
Import setuptools:
setuptools is a python package that is used to download, build, install, upgrade, and uninstall Python packages -- easily! For more information - http://pypi.python.org/pypi/setuptools
And as setuptools was not installed in my machine, I had to make it at first.
Check Installation:
First Script:
For more basics of Mysql programming with Python, you may visit
http://zetcode.com/databases/mysqlpythontutorial
I installed MySQL-python in my environment following the steps below.
Import setuptools:
setuptools is a python package that is used to download, build, install, upgrade, and uninstall Python packages -- easily! For more information - http://pypi.python.org/pypi/setuptools
And as setuptools was not installed in my machine, I had to make it at first.
wget http://pypi.python.org/packages/2.7/s/setuptools \Install MySQL-python:
/setuptools-0.6c11py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg
wget http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz
tar -zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build
python setup.py install
Check Installation:
# python
Python 2.7.1 (r271:86832, Jun 8 2011, 12:30:16)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import MySQLdb
>>
First Script:
vi mysql_test.pyExecute Script:
#!/usr/bin/python -tt
import MySQLdb as mdb
import sys
con = None
def main():
con = mdb.connect(host='localhost', user='testuser',passwd=t'test_passwd',db='testdb',port=3306)
cur = con.cursor()
cur.execute("SELECT VERSION()")
data = cur.fetchone()
print "Database version : %s " % data
return
if __name__ == '__main__':
main()
python mysql_test.py
Database version : 5.1.48-community-log
For more basics of Mysql programming with Python, you may visit
http://zetcode.com/databases/mysqlpythontutorial