Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SQL Server using SQLAlchemy

I'm trying to connect to a SQL Server Express database using SQLALchemy and pyodbc, but I'm continuously getting the error:

(pyodbc.Error) ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')

And I really don't understand if my engine url is wrong or what else. My scenario is the following:

  • I'm on a Mac
  • I have a docker container (based on a Debian image with unixodbc and unixodbc-dev) in which my python app tries to connect to...
  • a virtualbox virtual machine running windows 8 with SQL express 2014...

I configured a user for the SQL express, with SQL Server authentication:

  • user: ar_user
  • password: ar_psw

...then:

  • I configured TCP ports as 1433 and disabled dynamic ports (SQL Server Configuration Manager > Network Configurations > Protocols).
  • I turned off Windows Firewall.
  • I used an Host-only adapter for the VM running windows8

now...

The VM is accessible from the host (my mac), since a:

ping -c 3 vm-ip

succeed!

But although I tried every possible permutation of user, password, ip, server name and port:

  • 'mssql+pyodbc://ar_user:ar_psw@vm-ip/master'
  • 'mssql+pyodbc://ar_user:ar_psw@vm-ip:1433/master'
  • 'mssql+pyodbc://IE10WIN8\\SQLEXPRESS'
  • 'mssql+pyodbc://ar_user:ar_psw@IE10WIN8\\SQLEXPRESS'
  • 'mssql+pyodbc://ar_user:ar_psw@IE10WIN8\\SQLEXPRESS:1433'
  • 'mssql+pyodbc://ar_user:ar_psw@IE10WIN8\\SQLEXPRESS:1433/master'

...and many more!

I always get the "datasource not found error". What should I do?

ps: the vm is pingable even in the docker container!

UPDATE (solved but not 100%):

I solved in this way:

I configured FreeTDS driver using /etc/odbcinst.ini in this way:

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
client charset = UTF-8

and in /etc/freetds/freetds.conf:

[global]
tds version = 7.3
client charset = UTF-8

Then I created the engine using the following string:

'mssql+pyodbc://my_user:my_psw@machine_ip:1433/my_db?driver=FreeTDS'

It seems to work properly, but I get this warning:

SAWarning: Unrecognized server version info '95.12.255'. Version specific behaviors may not function properly. If using ODBC with FreeTDS, ensure TDS_VERSION 7.0 through 7.3, not 4.2, is configured in the FreeTDS configuration.

I also defined the TDS version using environment variables but it doesn't fix the issue... any idea?

like image 362
daveoncode Avatar asked Jan 10 '16 11:01

daveoncode


People also ask

Can you use SQLAlchemy with SQL Server?

SQLAlchemy supports these syntaxes automatically if SQL Server 2012 or greater is detected. Changed in version 1.4: support added for SQL Server “OFFSET n ROWS” and “FETCH NEXT n ROWS” syntax.

What SQL does SQLAlchemy use?

Supported Databases. SQLAlchemy includes dialects for SQLite, Postgresql, MySQL, Oracle, MS-SQL, Firebird, Sybase and others, most of which support multiple DBAPIs.


1 Answers

I wrote a tutorial here of how to do this. Essentially, you need to:

  1. brew install unixodbc
  2. brew install freetds --with-unixodbc
  3. Add the freetds driver to odbcinst.ini
  4. Add a DSN (Domain Source Name) to odbc.ini named "MY_DSN"
  5. pip install pyodbc
  6. e = create_engine("mssql+pyodbc://username:password@MY_DSN")

The walkthrough here does a much more thorough job of explaining this, including issues with SQL Server/FreeTDS Protocol Version Compatibility.

like image 135
The Aelfinn Avatar answered Sep 30 '22 09:09

The Aelfinn