1

I am using Ubuntu system and I want to connect to remote database SQL Express.

I have tried the same using pyodbc using following steps.

  1. sudo apt-get install python-pyodbc
  2. Python code as follows :

    import pyodbc
    cnxn = pyodbc.connect(DRIVER='{SQL Server}', SERVER='REMOTE_SERVER_IP', DATABASE='REMOTE_SERVER_DB_NAME', UID='redbytes', PWD='REMOTE_SERVER_DB_PASSWORD')
    cursor = cnxn.cursor()

But I am facing problem as follows :

pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Shraddha Bagde
  • 131
  • 2
  • 10
  • 1
    Possible duplicate of [Pyodbc - "Data source name not found, and no default driver specified"](http://stackoverflow.com/questions/16280304/pyodbc-data-source-name-not-found-and-no-default-driver-specified) – Dinesh Pundkar Aug 23 '16 at 06:10
  • 1
    If you're on Ubuntu, are you using the FreeTDS or the MS ODBC driver? You're going to need a driver (I'd recommend FreeTDS) to tell unixODBC how to connect to SQL Server. I'd also recommend against 'apt-get' installing any Python packages, they're very out of date; instead `pip install pyodbc` into a virtualenv. – FlipperPA Aug 23 '16 at 18:04
  • I am using FreeTDS and used pip install only – Shraddha Bagde Aug 24 '16 at 06:52

1 Answers1

1

pyodbc is just one part of the puzzle. It's only job is to allow your Python application to communicate with the ODBC subsystem ("Driver Manager"). That layer communicates with the ODBC driver, which in turn talks to the database.

In order to use Python_3 + pyodbc + unixODBC (ODBC driver manager) + FreeTDS (ODBC driver) on Ubuntu the basic steps are:

sudo su
apt install python3-pip unixodbc-dev tdsodbc
printf "\n[FreeTDS]\nDriver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n" >> /etc/odbcinst.ini 
exit
pip3 install --user pyodbc

then use DRIVER=FreeTDS in your ODBC connection string (along with SERVER=, and other required arguments).

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418