使用pyodbc连接到SQL服务器

时间:2022-01-07 01:44:08

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:

我需要用Python 2.7将unicode发送到SQL Server。我和pymssql失败。我现在尝试让pypyodbc工作(与pyodbc相反),因为它提供了工作的unicode示例。问题是示例中的连接字符串看起来不像我能识别的任何东西。我看着这个,经过一段时间的尝试和错误,我构建了这个字符串:

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?

我知道我的凭证是正确的,因为我使用它们成功地使用pymssql连接。我缺少什么?

2 个解决方案

#1


16  

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

从连接字符串的服务器、uid、pwd和数据库属性中删除单引号:

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:

由于pypyodbc提到了与pyodbc的兼容性,所以请花一分钟时间查看pyodbc连接字符串文档和pyodbc.connect()示例。我在pyodbc中使用了这种语法:

cnxn = connect(driver='{SQL Server}', server='localhost', database='test', uid='me', pwd='me2')

#2


3  

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.

在连接字符串中省略端口号(1433),从Linux客户机(但不是Windows 7)向我抛出错误。

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

把这个放在那里,以防它对别人有帮助。

#1


16  

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

从连接字符串的服务器、uid、pwd和数据库属性中删除单引号:

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:

由于pypyodbc提到了与pyodbc的兼容性,所以请花一分钟时间查看pyodbc连接字符串文档和pyodbc.connect()示例。我在pyodbc中使用了这种语法:

cnxn = connect(driver='{SQL Server}', server='localhost', database='test', uid='me', pwd='me2')

#2


3  

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.

在连接字符串中省略端口号(1433),从Linux客户机(但不是Windows 7)向我抛出错误。

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

把这个放在那里,以防它对别人有帮助。