尝试连接到本地主机上的数据库时出现 pyodbc 连接错误
我的机器上有一个名为“Test”的本地数据库,其中包含一个名为“Tags”的表。我可以通过 SQL Server Management Studio 2008 访问该数据库并从该表中进行查询。
但是,在使用 pyodbc 时,我不断遇到问题。
使用此:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
会产生错误:(
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]Invalid connection. (14) (SQLDriverConnectW); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()). (14)')
指定或不指定端口)
尝试替代连接字符串:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\Test,1433')
不会产生错误,但是:
cur = conn.cursor()
cur.execute("SELECT * FROM Tags")
会产生错误:
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Tags'. (208) (SQLExecDirectW)")
为什么会这样?
I have a local DB on my machine called 'Test' which contains a table called 'Tags'. I am able to access this DB and query from this table through SQL Server management studio 2008.
However, when using pyodbc I keep running into problems.
Using this:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
yields the error:
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]Invalid connection. (14) (SQLDriverConnectW); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()). (14)')
(with or without specifying the port)
Trying an alternative connection string:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost\Test,1433')
yields no error, but then:
cur = conn.cursor()
cur.execute("SELECT * FROM Tags")
yields the error:
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Tags'. (208) (SQLExecDirectW)")
Why could this be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我尝试将您的查询更改为
并且它有效。
I tried changing your query to
and it worked.
我在您的连接字符串中没有看到任何身份验证属性。试试这个(我使用的是 Windows 身份验证):
I don't see any authentication attributes in your connection strings. Try this (I'm using Windows authentication):
尝试将“localhost”替换为“(本地)”或“.”。这个解决方案为我解决了问题。
Try replacing 'localhost' with either '(local)' or '.'. This solution fixed the problem for me.
对于我来说,除了维护连接详细信息(用户、服务器、驱动程序、正确的表名称等)之外,
我还采取了以下步骤:
参考链接:此处
For me, apart from maintaining the connection details (user, server, driver, correct table name etc.),
I took these steps:
Reference Link: here
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
此连接缺少实例名称和端口,不应该这样写。
我的连接是这样的:
在此处输入图像描述
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost:1433;DATABASE=Test')
This connection lack of instance name and the port shouldn't be writen like this.
my connection is this:
enter image description here