Knowledgebase: MySQL & Databases
How to connect with Python
Posted by Pacific Host on 05 December 2011 05:42 AM

How do I connect to a MySQL database using Python? 
First, import the MySQLdb module:

import MySQLdb

Next, create a connection object:

connection=MySQLdb.connect(host="localhost",db="<database_name>",user="<username>",passwd="<password>")

Finally, create a cursor object:

cursor=connection.cursor()

Use the 'execute' method on the 'cursor' object to submit SQL to the database.

(0 vote(s))
Helpful
Not helpful

Comments (0)