python - 使用 pyodbc 连接到数据库 - 不起作用

发布于 2024-11-16 22:24:55 字数 1228 浏览 3 评论 0原文

我正在尝试使用以下连接字符串连接到 Access 2007 数据库。

conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=<C:\Users\JSeinfeld\Desktop\Backup databases\Database_6.7.accdb>;")

我收到此错误:

回溯(最近一次调用最后一次):
文件“”,第 1 行,位于 conn = pyodbc.connect("驱动程序={Microsoft 访问驱动程序(*.mdb、 *.accdb)};DBQ=;")

错误:('HY000',“[HY000] [微软][ODBC Microsoft Access 驱动程序]一般错误无法打开 注册表项临时(易失)Ace 进程 0x167c 线程 0x1568 的 DSN DBC 0x1c67a5c

喷气机”。 (63) (SQLDriverConnectW); [HY000] [微软][ODBC 微软 访问驱动程序]一般错误无法 打开注册表项临时(易失) 进程 0x167c 线程的 Ace DSN 0x1568 DBC 0x1c67a5c

喷气机”。 (63); [HY000] [微软][ODBC Microsoft Access 驱动程序] 无效 文件名。 (-1044); [HY000] [微软][ODBC Microsoft Access 驱动程序]一般错误无法打开 注册表项临时(易失)Ace 进程 0x167c 线程 0x1568 的 DSN DBC 0x1c67a5c

喷气机”。 (63); [HY000] [微软][ODBC Microsoft Access 驱动程序]一般错误 临时无法打开注册表项 (易失性)进程 0x167c 的 Ace DSN 线程 0x1568 DBC 0x1c67a5c

喷气机”。 (63); [HY000] [微软][ODBC Microsoft Access 驱动程序] 无效 文件名。 (-1044)")

还有一个类似的问题,但是有一个我没有的 32/64 位兼容性问题。这个问题似乎没有一个好的答案,但希望有人可以帮助我连接到我的数据库,因为我不知道使用 python 查询或写入数据到 Access 2007 的任何其他方法

I am trying to connect to an Access 2007 database with the following connection string.

conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=<C:\Users\JSeinfeld\Desktop\Backup databases\Database_6.7.accdb>;")

I get this error:

Traceback (most recent call last):
File "", line 1, in

conn = pyodbc.connect("Driver={Microsoft
Access Driver (*.mdb,
*.accdb)};DBQ=;")

Error: ('HY000', "[HY000]
[Microsoft][ODBC Microsoft Access
Driver]General error Unable to open
registry key Temporary (volatile) Ace
DSN for process 0x167c Thread 0x1568
DBC 0x1c67a5c

Jet'. (63) (SQLDriverConnectW);
[HY000] [Microsoft][ODBC Microsoft
Access Driver]General error Unable to
open registry key Temporary (volatile)
Ace DSN for process 0x167c Thread
0x1568 DBC 0x1c67a5c

Jet'. (63); [HY000] [Microsoft][ODBC
Microsoft Access Driver] Not a valid
file name. (-1044); [HY000]
[Microsoft][ODBC Microsoft Access
Driver]General error Unable to open
registry key Temporary (volatile) Ace
DSN for process 0x167c Thread 0x1568
DBC 0x1c67a5c

Jet'. (63); [HY000] [Microsoft][ODBC
Microsoft Access Driver]General error
Unable to open registry key Temporary
(volatile) Ace DSN for process 0x167c
Thread 0x1568 DBC 0x1c67a5c

Jet'. (63); [HY000] [Microsoft][ODBC
Microsoft Access Driver] Not a valid
file name. (-1044)")

There is another question like this on SO, but there was a 32/64bit compatibility issue which I do not have. There doesn't seem to be a good answer for this question, but hopefully someone can help me connect to my database because I don't know of any other way to query or write data to Access 2007 with python.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木格 2024-11-23 22:24:55

遇到了同样的问题 - 这是转义访问数据库位置中使用的反斜杠的简单情况

>>> conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\access\\site_be.accdb;")

的工作方式与

>>> conn = pyodbc.connect("Driver={Microsoft Access 驱动程序 (*.mdb, *.accdb)};DBQ=C:\access\site_be.accdb;")

给了我错误
<代码>喷气机'。 (63); [HY000] [Microsoft][ODBC Microsoft Access 驱动程序] 不是有效的文件名。 (-1044)")
希望

有帮助..

Had the same issue - it was a simple case of escaping the backslash used in the location of the access db

>>> conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\access\\site_be.accdb;")

worked as whereas

>>> conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\access\site_be.accdb;")

was giving me the error
Jet'. (63); [HY000] [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. (-1044)")

Hope that helped..

独留℉清风醉 2024-11-23 22:24:55

尝试去掉文件名中的括号。

conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\JSeinfeld\Desktop\Backup databases\Database_6.7.accdb")

Try taking the brackets out of the file name.

conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\JSeinfeld\Desktop\Backup databases\Database_6.7.accdb")
亽野灬性zι浪 2024-11-23 22:24:55

您的文件夹中有 *.ldb 文件吗?

我有非常相似的错误消息,但我的错误消息还包括“文件已在使用中”。就我而言,我最终意识到数据库上有一个锁(.mdb 旁边的 .ldb 文件)。我有一个一直在使用数据库的服务正在运行——我想是时候切换到多用户数据库引擎了。

作为实验,我制作了数据库的副本,并且 pyodbc 连接到该副本效果很好。这显然不是一个长期的解决方案,但至少现在我知道问题出在哪里。

Is there an *.ldb file in your folder?

I had very similar error messages, but mine also included "file already in use." In my case, I finally realized that there was a lock on the database (an .ldb file next to the .mdb). I have a service running that uses the database all the time -- I guess it's time to switch to a multi-user database engine.

As an experiment, I made a copy of the database, and pyodbc connected to the copy just fine. This obviously isn't a long-term solution, but at least now I know what the problem was.

丑疤怪 2024-11-23 22:24:55

法比卢斯说得对。

反斜杠字符表示下一个字符在字符串中具有特殊含义。所以破译双反斜杠...第一个反斜杠告诉Python准备特殊字符串字符,第二个反斜杠是特殊字符串字符(在本例中是反斜杠)

\n = newline
\t = tab
\\ = backslash

Fabiolus got it right.

The backslash character means that the next character has special meaning in a string. So deciphering the double backslash... The first backslash tells python to prepare for a special string character, the second backslash is the special string character (which in this case is a backslash)

\n = newline
\t = tab
\\ = backslash
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文