Linux 使用 Python 读取 Microsoft Access 数据库需要什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/853370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:18:51  来源:igfitidea点击:

What do I need to read Microsoft Access databases using Python?

pythonlinuxms-accesspython-module

提问by Georg Sch?lly

How can I access Microsoft Access databases in Python? With SQL?

如何在 Python 中访问 Microsoft Access 数据库?用SQL?

I'd prefere a solution that works with Linux, but I could also settle for Windows.

我更喜欢一个适用于 Linux 的解决方案,但我也可以适应 Windows。

I only require read access.

我只需要读取权限。

采纳答案by Alex Boschmans

I've used PYODBCto connect succesfully to an MS Access db - on Windows though. Install was easy, usage is fairly simple, you just need to set the right connection string (the one for MS Access is given in the list) and of you go with the examples.

我已经使用PYODBC成功连接到Windows 上的 MS Access 数据库。安装很简单,使用方法也很简单,您只需要设置正确的连接字符串(列表中给出了 MS Access 的连接字符串),然后按照示例进行操作。

回答by stuartd

How about pyodbc? This SO questiondemonstrates it's possible to read MS Access using it.

pyodbc怎么样?这个 SO 问题表明可以使用它来读取 MS Access。

回答by Nico

Most likely, you'll want to use a nice framework like SQLAlchemyto access your data, or at least, I would recommend it. Support for Accessis "experimental", but I remember using it without too many problems. It itself uses pyodbcunder the hood to connect to Access dbs, so it should work from windows, linux, os x and whatnot.

最有可能的是,您会想要使用像SQLAlchemy这样的好框架来访问您的数据,或者至少,我会推荐它。对 Access 的支持是“实验性的”,但我记得使用它没有太多问题。它本身在引擎盖下使用pyodbc连接到 Access dbs,因此它应该可以在 windows、linux、os x 和诸如此类的东西上工作。

回答by apenwarr

If you sync your database to the web using EQL Data, then you can query the contents of your Access tables using JSON or YAML: http://eqldata.com/kb/1002.

如果您使用EQL Data将数据库同步到 Web ,那么您可以使用 JSON 或 YAML 查询 Access 表的内容:http: //eqldata.com/kb/1002

That article is about PHP, but it would work just as well in Python.

那篇文章是关于 PHP 的,但它在 Python 中也能正常工作。

回答by Tony Toews

You've got what sounds like some good solutions. Another one that might be a bit closer to the "metal" than you'd like is MDB Tools.

您已经获得了一些听起来不错的解决方案。另一个可能比您想要的更接近“金属”的是 MDB 工具。

MDB Toolsis a set of open source libraries and utilities to facilitate exporting data from MS Access databases (mdb files) without using the Microsoft DLLs. Thus non Windows OSs can read the data. Or, to put it another way, they are reverse engineering the layout of the MDB file.

MDB 工具是一组开源库和实用程序,用于在不使用 Microsoft DLL 的情况下促进从 MS Access 数据库(mdb 文件)导出数据。因此非 Windows 操作系统可以读取数据。或者,换句话说,他们正在对 MDB 文件的布局进行逆向工程。

Also note that I doubt they've started working on ACCDB files and there is likely not going to be much request for that capability.

另请注意,我怀疑他们是否已经开始处理 ACCDB 文件,并且可能不会有太多对该功能的要求。

回答by pypyodbc

On Linux, MDBTools is your only chance as of now. [disputed]

在 Linux 上,MDBTools 是您目前唯一的机会。[争议]

On Windows, you can deal with mdb files with pypyodbc.

在 Windows 上,您可以使用 pypyodbc 处理 mdb 文件。

To create an Access mdb file:

创建 Access mdb 文件:

import pypyodbc
pypyodbc.win_create_mdb( "D:\Your_MDB_file_path.mdb" )

Here is an Hello World scriptthat fully demostate pypyodbc's Access support functions.

这是一个 Hello World 脚本,它完全演示了 pypyodbc 的 Access 支持功能。

Disclaimer: I'm the developer of pypyodbc.

免责声明:我是 pypyodbc 的开发者。

回答by Erik Knowles

Old question, but I thought I'd post a pypyodbc alternative suggestion for Windows: ADO. Turns out, it's really easy to get at Access databases, Excel spreadsheets and anything else with a modern (as opposed to old-school ODBC) driver via COM.

老问题,但我想我会为 Windows 发布一个 pypyodbc 替代建议:ADO。事实证明,使用现代(而不是老式 ODBC)驱动程序通过 COM 访问 Access 数据库、Excel 电子表格和其他任何东西真的很容易。

Check out the following articles:

查看以下文章:

回答by Menda

On Ubuntu 12.04 this was what I did to make it work.

在 Ubuntu 12.04 上,我就是这样做的。

Install pyodbc:

安装pyodbc:

$ sudo apt-get install python-pyodbc

Follow on installing some extra drivers:

继续安装一些额外的驱动程序:

$ sudo apt-get install mdbtools libmdbodbc1

Make a little test program which connects to the DB and displays all the tables:

制作一个连接到数据库并显示所有表的小测试程序:

import os
import pyodbc

db_path = os.path.join("path", "toyour", "db.mdb")
odbc_connection_str = 'DRIVER={MDBTools};DBQ=%s;' % (db_path)
connection = pyodbc.connect(odbc_connection_str)
cursor = connection.cursor()

query = "SELECT * FROM MSysObjects WHERE Type=1 AND Flags=0"
cursor.execute(query)
rows = cursor.fetchall()
for row in rows:
    print row

I hope it helped.

我希望它有所帮助。

回答by ankostis

If you have some time to spare, you can try to fix and update this python-class that reads MS-Access DBs through the native COM32-client API: Extraction and manipulation class for Microsoft Access

如果您有空闲时间,您可以尝试修复和更新这个通过原生 COM32 客户端 API 读取 MS-Access 数据库的 Python 类:Microsoft Access 的提取和操作类

回答by Gord Thompson

Personally, I have never been able to get MDB Tools (along with related ODBC stuff like unixODBC) to work properly with Python or PHP under Linux, even after numerous attempts. I just tried the instructions in the other answer to this question hereand all I got was "Segmentation fault (core dumped)".

就我个人而言,即使经过多次尝试,我也一直无法让 MDB 工具(以及相关的 ODBC 工具,如 unixODBC)在 Linux 下与 Python 或 PHP 一起正常工作。我只是在这里尝试了这个问题的另一个答案中的说明,我得到的只是“分段错误(核心转储)”。

However, I did get the UCanAccessJDBC driver to read both .mdb and .accdb files on Linux from either Jython or CPython+JayDeBeApi. For detailed instructions on how I set it up under Ubuntu 14.04 LTS see my other answer here.

但是,我确实让UCanAccessJDBC 驱动程序从 Jython 或 CPython+JayDeBeApi 读取 Linux 上的 .mdb 和 .accdb 文件。有关如何进行设置了在Ubuntu下详细的说明14.04 LTS看到我的其他答案在这里