7

I am trying to connect to a datbase using pyodbc and running into following error,can anyone suggest how to overcome below error?

Installed pyodbc using the following commands

sudo apt-get install unixodbc-dev
pip install pyodbc

Code:-

#!/usr/bin/python
import pyodbc

server_name='odsdb.qualcomm.com'
database_name='ODS'
#cnx = pyodbc.connect("SERVER="+server_name+";DATABASE="+database_name)
cnx = pyodbc.connect("DRIVER={SQL Server};SERVER="+server_name+";DATABASE="+database_name)

db_cursor = cnx.cursor() 

print "List of tables in DB"
for row in db_cursor.tables():
    print row.table_name
print "\n"

Error:-

Traceback (most recent call last):
  File "mysql.py", line 7, in <module>
    cnx = pyodbc.connect("DRIVER={SQL Server};SERVER="+server_name+";DATABASE="+database_name)
3
  • Is your server reachable on given address? Does it allow access without UID and PWD? Commented May 28, 2014 at 0:09
  • yes,it is reachable, is there anyohter wya to conneect to server_name='odsdb.company.com' database_name='ODS' Commented May 28, 2014 at 0:17
  • Some command line tool? I found something about odbcinst etc., but this is definitely not something, I can test on my box today. Sorry Commented May 28, 2014 at 5:40

1 Answer 1

1

You should start by setting up and configuring FreeTDS. Here is a sample configurations from my files, but I'm sure other variants will work also. One difference is that I'm using Django, but the result below still worked eventually, but it works much better with SQL authentication than with Windows Authentication.

From /etc/freetds/freetds.conf (use the IP of the server if DNS is not active for the server name).

# A typical Microsoft server
[MyServer]
        host = 10.0.0.10\path
        port = 1433
        tds version = 7.0

From /etc/odbcinst.ini

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

From /etc/odbc.ini

[MyServer]
Description = "Some Description"
Driver = FreeTDS
ServerName = servername\path
Server = servername
Port = 1433
Database = DBNAME

Then this command connects me to the database.

tsql -S MyServer -U username@servername -P password

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

1 Comment

Having same issue OP has. I have configured those 3 files like you have, and I can use tsql to connect and run SQL queries, but I still get the same error. I replaced MyServer with SQL Server. What haven't I done?

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.