3

I'm using the pyodbc library to connect to a remote instance of sql server on windows from a python djanog web app running on an ubuntu Vm.

I have a database connection class, as below, which breaks on the line where I try and create a connection of object (there are a number of connectionStrings I've been trying);

import pyodbc


class SQLSeverConnection():

def __init__(self, DSN, user, password, database):
    connectionString = 'DSN=MSSQLServerDataSource;UID=django;PWD=password123!;DATABASE=HD'

    self.connection = pyodbc.connect(connectionString)
    self.cursor = self.connection.cursor()

def getTableNames(self):
    self.cursor.execute('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\'')
    tables = self.cursor.fetchall()
    return tables

def getColumnTitles(self, tableName):
    self.cursor.execute("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "' ORDER BY ORDINAL_POSITION")
    columns = self.cursor.fetchall()
    return columns

def getColumnData(self, columnName, tableName, startDateTime, endDateTime):
    self.cursor.execute('SELECT ' + columnName + ' FROM ' + tableName + ' BETWEEN ' + startDateTime + ' AND ' + endDateTime + ' ORDER BY timestamp')
    data = self.cursor.fetchall()
    return data

When I runserver I get the error as described in the title.

My configuration files are as below;

(odbc.ini)
[MSSQLServerDataSource]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = MSSQLServer
Database = HD
TDS_Version = 8.0

(odbcinst.ini)
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
# Some installations may differ in the paths
#Driver = /usr/lib/odbc/libtdsodbc.so
#Setup = /usr/lib/odbc/libtdsS.so
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 4

(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

#Server For Django App
[MSSQLSever]
   host = <ip>
   port = 1433
   tds version = 8.0

When I type odbcinst -j into the terminal;

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/user/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

The connection succeeds from the command line using isql and the dsn, user and password.

I really don't know what to do, I've been going round in circles with this for over a day now. Some help would be greatly appreciated!

1
  • 1
    I had to specify PORT=1433 and TDS_Version=7.2 in the connection string. Commented Jan 28, 2016 at 4:39

1 Answer 1

4

Try this:

connectionString = 'DRIVER={FreeTDS};SERVER=10.1.3.230;PORT=1433;DATABASE=HD;UID=django;PWD=password123!;TDS_Version=7.2;'

You need to specify the TDS Version explicitly. See the different TDS Versions you can use here, 7.2 has worked for 2008+ for me on every SQL Server through 2014:

http://www.freetds.org/userguide/choosingtdsprotocol.htm

You'll need to do the same in your Django pyodbc settings. I'd recommend using this version of django-pyodbc:

https://github.com/lionheart/django-pyodbc/

Best of luck, let me know if it works for you.

Sign up to request clarification or add additional context in comments.

2 Comments

hi, turns out the issue was a mis-spelling in my freetds.conf file. i had misspelled my server name as MSSQLSever! Now onto the next problem...
As my friend would say, "All errors are clerical!" Good job tracking it down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.