Animesh Kumar {itsAnimesh}

Animesh Kumar {itsAnimesh}

I'm an IT professional from New Delhi, India. Currently for the most part I work as a Technical Consultant developing Open Source ICT solutions for social and societal inclusion. I'm also pursing PG in Management. Below are the latest updates from a some social networks I subscribe to.

Access MSSQL with Python!!!

I started using Python a while ago and one of the things I need to do is to be able to connect to Microsoft SQL server from Linux servers… Its suprising that only a few projects are alive for it… One of them is the commercial MxODBC from eGenix, while the other is called pymssql… I gave pymssql a try and it worked almost at once on my Ubuntu machine…

First I installed the prerequisites: freetds and the python headers:

aptitude install python2.5-dev freetds-dev

There might be some other packages that are needed but that was the only ones I needed to install…. Then I decompressed the latest release from pymssql at sourceforge and installed:

NOTE : for me the latest package did not work so I used pymssql-0.8.0

tar -xvzf pymssql-0.8.0.tar.gz
cd pymssql-0.8.0
python setup.py install

Now you can test it out by creating your own test script and it is as follows :

import pymssql
conn = pymssql.connect(host='<hostname>', user='<username>', password='<passwd>', database='<dbname>')
cur = conn.cursor()
cur.execute('SELECT * FROM tbABCD')
row = cur.fetchone()
while row:
 print "ID=%s, Name=%s, Address=%s" % (row[0], row[1], row[2])
 row = cur.fetchone()
conn.close()

The script ran perfectly…
For more information on pymssql you can view its documentation here

Thanks for Reading... Please Post Your Comments Below