Skip to content

Instantly share code, notes, and snippets.

@89465127
Forked from rduplain/README.md
Last active December 19, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 89465127/5887859 to your computer and use it in GitHub Desktop.
Save 89465127/5887859 to your computer and use it in GitHub Desktop.

Goal: Connect to MSSQL using FreeTDS / ODBC in Python.

Host: Linux Mint 15 x86_64

Install:

sudo apt-get install python-dev python-pip freetds-dev freetds-bin unixodbc-dev tdsodbc
pip install pyodbc

In /etc/odbcinst.ini:

[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

In /etc/odbc.ini:

[FreeTDS]
Description = "test"
Driver = FreeTDS
Server = my-server
Port = 1433

In /etc/freetds/freetds.conf:

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
;    tds version = 4.2

	# Whether to write a TDSDUMP file for diagnostic purposes
	# (setting this to /tmp is insecure on a multi-user system)
;	dump file = /tmp/freetds.log
;	debug flags = 0xffff

	# Command and connection timeouts
;	timeout = 10
;	connect timeout = 10
	
	# If you get out-of-memory errors, it may mean that your client
	# is trying to allocate a huge buffer for a TEXT field.  
	# Try setting 'text size' to a more reasonable limit 
	text size = 64512

[FreeTDS]
        host = my-server
        port = 1433
        tds version = 8.0
"Proof connection at pyodbc level."
# Test pyodbc connection. Result is 42.
# Note parameters in connection string, <PARAMETER>.
import pyodbc
conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
cursor = conn.cursor()
for row in cursor.execute('select 6 * 7 as [Result];'):
print row.Result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment