Python Tutorial
From Hashmysql
To connect to mysql with python, you will need the MySQLdb module for python. Get this module at sourceforge: MySQLdb Module
Install the module like this:
$ tar -xzvf MySQL-python-*.tar.gz $ cd MySQL-python-* $ python setup.py build $ python setup.py install
Now when you are in the python terminal and write:
import MySQLdb
you should get no errors.
Now to get some data out of a MySQL database...
#!/usr/bin/python import MySQLdb # connect db = MySQLdb.connect(host="localhost", user="myuser", passwd="mypass", db="mydatabase") # Since we would like to return a dictionary instead of a list we use the # DictCursor instead of db.createCursor() which would give us a numeric index only cursor = MySQLdb.cursors.DictCursor(db) # execute SQL statement cursor.execute("SELECT * FROM my_table") # get the results result = cursor.fetchallDict() # iterate over the results for record in result: print record['my_col']
That should get you started with Python and MySQL!