Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server with pypyodbc

I need to send unicode to SQL Server with Python 2.7. I failed with pymssql. I'm now trying to get pypyodbc working (as opposed to pyodbc), as it gives working unicode examples. The problem is that the connection string in the example doesn't look like anything I recognize. I looked at this, and, after a little trial and error, constructed this string:

conn = pypyodbc.connect("DRIVER={SQL Server};SERVER='MyServer';UID='me';PWD='MyPassword';DATABASE='db'")

Got back a DatabaseError focused on the connection string:

C:\Anaconda\lib\site-packages\pypyodbc.pyc in __init__(self, connectString, autocommit, ansi, timeout, unicode_results, readonly, **kargs)
---> 2 conn = pypyodbc.connect("DRIVER={SQL Server};SERVER='MyServer';UID='me';PWD='password';DATABASE='db'")

C:\Anaconda\lib\site-packages\pypyodbc.pyc in __init__(self, connectString, autocommit, ansi, timeout, unicode_results, readonly, **kargs)
---> 2273         self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)

C:\Anaconda\lib\site-packages\pypyodbc.pyc in connect(self, connectString, autocommit, ansi, timeout, unicode_results, readonly)
---> 2321         check_success(self, ret)

C:\Anaconda\lib\site-packages\pypyodbc.pyc in ctrl_err(ht, h, val_ret, ansi)
---> 919                 raise DatabaseError(state,err_text)

DatabaseError: (u'08001', u'[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.')

I know my credentials are correct because I've used them to connect successfully using pymssql. What am I missing?

like image 259
scharfmn Avatar asked Apr 15 '13 21:04

scharfmn


People also ask

How do I connect to a SQL Server connection?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).


2 Answers

Remove the single quotes from the server, uid, pwd, and database attributes of the connection string:

conn = pypyodbc.connect("DRIVER={SQL Server};SERVER=MyServer;UID=me;PWD=password;DATABASE=db")

Since pypyodbc mentions compatibility with pyodbc, take a minute to look over the pyodbc connection string docs and pyodbc.connect() examples. I use this syntax in pyodbc:

cnxn = connect(driver='{SQL Server}', server='localhost', database='test', uid='me', pwd='me2')
like image 52
Bryan Avatar answered Oct 23 '22 14:10

Bryan


Leaving out the port number (1433) in the connection string, threw errors at me from a Linux client (but not Windows 7). It's probably a configuration issue but I didn't have time to chase it.

Putting this out there, in case it helps someone else.

like image 40
peter n Avatar answered Oct 23 '22 12:10

peter n