使用pypyodbc连接到SQL Server [英] Connecting to SQL Server with pypyodbc
问题描述
我需要使用python 2.7将unicode发送到SQL Server.我 pymssql失败.我现在正尝试使pypyodbc
工作(而不是pyodbc
),因为它提供了此,经过一番尝试和错误之后,构造了这个字符串:
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'")
找回专注于连接字符串的DatabaseError
:
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.')
我知道我的凭据是正确的,因为我已使用它们使用pymssql成功连接.我想念什么?
I know my credentials are correct because I've used them to connect successfully using pymssql. What am I missing?
推荐答案
从连接字符串的server
,uid
,pwd
和database
属性中删除单引号:
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")
由于pypyodbc
提到与pyodbc
的兼容性,请花点时间查看 pyodbc连接字符串docs 和 pyodbc.connect()示例.我在pyodbc中使用了这种语法:
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')
这篇关于使用pypyodbc连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!