Monday, March 25, 2013

Using python pymssql to query SQLServer from linux

Getting pyssmql to work was pretty easy. (I had less luck using pyodbc.)

sudo apt-get install freetds

sudo pip install pymssql

Create entries for your sqlservers in /etc/freetds/freetds.conf like so:

port = 1433
tds version = 8.0

Here's a simple script using it:

import pymssql
import getpass

pwd = getpass.getpass()

conn = pymssql.connect(host='YOUR_SQL_SERVER_HOST',
                       user='YOUR_DOMAIN\\YOUR_USERNAME', password=pwd)
cur = conn.cursor()
cur.execute('use YOUR_DB')

query = "select top 10 id, name, lastmodifieddate from campaign"


for row in cur:
    print "{0}\n{1}\n{2}\n".format(row[0], row[1], row[2])

No comments:

Post a Comment