Connect to SQL Server using SQLAlchemy

I am trying to connect to a SQL Server Express database using SQLALchemy and pyodbc, but I constantly get an error message:

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

And I really don't understand if my engine URL is wrong or what else. My scenario is as follows:

  • I'm on a mac
  • I have a docker container (based on a Debian image with unixodbc and unixodbc-dev ) in which my python application is trying to connect to ...
  • virtualbox virtual machine running Windows 8 with SQL express 2014 ...

I configured the user for SQL Express using 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 disabled the windows firewall.
  • I used the Host-only adapter for a virtual machine running Windows8

Now...

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

 ping -c 3 vm-ip 

will succeed!

But although I tried all possible permutations of the 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 much more!

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

ps: vm pingable even in docker container!

UPDATE (allowed, but not 100%):

I decided this way:

I configured the FreeTDS driver using /etc/odbcinst.ini as follows:

 [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 line:

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

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

SAWarning: version information for the unrecognized server '95 .12.255 '. Version specific behavior may not work properly. If you are using ODBC with FreeTDS, make sure that TDS_VERSION 7.0 through 7.3, not 4.2, is configured in FreeTDS Configuration.

I also defined the TDS version using environment variables, but this does not fix the problem ... any idea?

+5
source share
2 answers

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

  • brew install unixodbc
  • brew install freetds --with-unixodbc
  • Add freetds driver to odbcinst.ini
  • Add DSN (domain source name) to odbc.ini with the name "MY_DSN"
  • pip install pyodbc
  • e = create_engine("mssql+pyodbc://username: password@MY _DSN")

Walking through here provides much more detailed information about this, including compatibility issues with SQL Server / FreeTDS protocol versions.

+3
source

Most likely, it is too late, but if someone has similar problems, perhaps the link below will help: Connecting to SQL Server from SQL Alchemy

0
source

Source: https://habr.com/ru/post/1240269/


All Articles