Connect to Database Using Python

Although you can connect to databases using Python on many platforms, the specific examples here are on Ubuntu.


If you would like to connect to MySQL using Python, the process is quite simple. You will need MySQLdb. In Ubuntu, it is quite easy to install

sudo apt-get install python-mysqldb

Once you have installed it, you are ready to go. How do you actually connect to it? I would rather let people smarter than me explain it. One such resource is Using the MySQLdb Interface.

Microsoft SQL Server

A fantastic guide for beginners has been provided at Accessing MSSQL using Python on Ubuntu. Head over there to read how to do it. Some useful links: pymssql, freetds, and ubuntu freetds.

However, I can also try to customize install instructions here.

sudo apt-get install python2.5-dev freetds-dev build-essential

You have to download the pymssql module, but choose the platform independent files, not Windows specific. After downloading (I am assuming the .zip file), you may install it using the following steps:


cd pymssql-0.8.0

sudo python install

To see how to use it in your scripts, try Example script – pymssql module, Example script – _mssql module, or Accessing MSSQL using Python on Ubuntu.


2 Responses to Connect to Database Using Python

  1. Jonathan says:

    I love tutorials that explain nothing and direct you to someone else’s tutorial.

  2. TGM says:

    If you know of a python object that can handle all popular databases, I’d like to hear of it…

%d bloggers like this: