Jacob Jacob - 26 days ago 8
Python Question

Python --> MSSQL

I'm at a loss here. I'm very new to python and odbc in general. However I need to find a way to have a python script in the backend of a web gui for an internal tool for my company, communicate with MSSQL server from: 1. my local mac os machine, 2.a linux server that hosts all of our internal tools.

I have tried installing freetds and configuring it as show:
http://blog.nguyenvq.com/blog/2013/04/06/guide-to-accessing-ms-sql-server-and-mysql-server-on-mac-os-x/

but I'm getting issues with driver not being located where the tutorial specifies after installing freetds.

I'm not quite sure where to even start with troubleshooting.

Can anyone give me a place to start with possibly making this work? It seemed to me python was going to be my best bet with this, but if there are other, better options -- I'm certainly open to them.

Thanks so much, I apologize for how open ended this is, but I'm not sure where to start. Every resource I'm finding is either miles over my head, or is too ambiguous to follow.

Answer

I'm not sure how to handle the OSx part of this, but you might be able to do something similar.

I have successfully used pyodbc (https://code.google.com/p/pyodbc/) to connect to SQL Server 2008.

Steps for RHEL/CentOS 6 (taken from http://funwithlinux.net/2013/07/connect-to-sql-server-with-python/)

1)Enable the EPEL

2)Install the Required Packages

yum install gcc gcc-c++ python-devel freetds unixODBC unixODBC-devel

3)Download latest stable version of pyodbc

4)Unzip, then build and install pyodbc with the following command:

python setup.py build install

5)edit /etc/odbcinst.ini to include the following:

[FreeTDS]
Driver = /usr/lib64/libtdsodbc.so.0
UsageCount = 1

6)Test with the following example (modified to your needs, obviously):

import pyodbc

cnxn = pyodbc.connect('DRIVER={FreeTDS};SERVER=dev-sql02;PORT=1433;UID=EXAMPLE\\myusero;PWD=xxx;DATABASE=fx_staging;UseNTLMv2=yes;TDS_Version=8.0;Trusted_Domain=EXAMPLE;')
cursor = cnxn.cursor()
cursor.execute("select state, zip from addresses")
row = cursor.fetchone()
if row:
    print row