How to Connect MySQL database with Python

Hi…to all today i’m going to tell how to connect python with Mysql

we need a package named MySQLdb

install that package inorder to connect mysql and python.

$ sudo apt-get install python-mysqldb

Here is the code to connect to mysql

# !usr/bin/python
import MySQLdb
#creating connection using Mysqldb and mqsql dbname,username and password
con = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “”, db = “balaji”)
#creating currsor to access to db
#Executing query using cursor
cursor.execute(“SELECT VERSION()”)
#printing the result
print result
#closing the db connection .

Save this file with .py extension

now run the file by $ python

Thats it..enjoy the coding with python…happy times!!!

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

One thought on “How to Connect MySQL database with Python”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: