Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server 2012 using sqlalchemy and pyodbc

I'm trying to connect to a SQL Server 2012 database using SQLAlchemy (with pyodbc) on Python 3.3 (Windows 7-64-bit). I am able to connect using straight pyodbc but have been unsuccessful at connecting using SQLAlchemy. I have dsn file setup for the database access.

I successfully connect using straight pyodbc like this:

con = pyodbc.connect('FILEDSN=c:\\users\\me\\mydbserver.dsn') 

For sqlalchemy I have tried:

import sqlalchemy as sa engine = sa.create_engine('mssql+pyodbc://c/users/me/mydbserver.dsn/mydbname') 

The create_engine method doesn't actually set up the connection and succeeds, but iIf I try something that causes sqlalchemy to actually setup the connection (like engine.table_names()), it takes a while but then returns this error:

DBAPIError: (Error) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)') None None

I'm not sure where thing are going wrong are how to see what connection string is actually being passed to pyodbc by sqlalchemy. I have successfully using the same sqlalchemy classes with SQLite and MySQL.

Thanks in advance!

like image 345
Brad Campbell Avatar asked Apr 01 '13 19:04

Brad Campbell


People also ask

Does SQLAlchemy work with SQL Server?

SQLAlchemy supports these syntaxes automatically if SQL Server 2012 or greater is detected.

Which is better Pyodbc or SQLAlchemy?

PyODBC allows you connecting to and using an ODBC database using the standard DB API 2.0. SQL Alchemy is a toolkit that resides one level higher than that and provides a variety of features: Object-relational mapping (ORM) Query constructions.

How do I run SQL query from SQLAlchemy in Python?

Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below, create a table called books with columns book_id and book_price. Insert record into the tables using insert() and values() function as shown.


2 Answers

The file-based DSN string is being interpreted by SQLAlchemy as server name = c, database name = users.

I prefer connecting without using DSNs, it's one less configuration task to deal with during code migrations.

This syntax works using Windows Authentication:

engine = sa.create_engine('mssql+pyodbc://server/database') 

Or with SQL Authentication:

engine = sa.create_engine('mssql+pyodbc://user:password@server/database') 

SQLAlchemy has a thorough explanation of the different connection string options here.

like image 156
Bryan Avatar answered Sep 30 '22 08:09

Bryan


In Python 3 you can use function quote_plus from module urllib.parse to create parameters for connection:

import urllib params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"                                  "SERVER=dagger;"                                  "DATABASE=test;"                                  "UID=user;"                                  "PWD=password")  engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params)) 

In order to use Windows Authentication, you want to use Trusted_Connection as parameter:

params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"                                  "SERVER=dagger;"                                  "DATABASE=test;"                                  "Trusted_Connection=yes") 

In Python 2 you should use function quote_plus from library urllib instead:

params = urllib.quote_plus("DRIVER={SQL Server Native Client 11.0};"                            "SERVER=dagger;"                            "DATABASE=test;"                            "UID=user;"                            "PWD=password") 
like image 32
pooja karande Avatar answered Sep 30 '22 09:09

pooja karande