简体   繁体   English

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

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

How can I access Microsoft Access databases in Python? 如何使用Python访问Microsoft Access数据库? With SQL? 用SQL吗?

I'd prefere a solution that works with Linux, but I could also settle for Windows. 我希望可以在Linux上使用的解决方案,但也可以选择Windows。

I only require read access. 我只需要读取权限。

On Linux, MDBTools is your only chance as of now. 在Linux上,到目前为止,您仅有的机会就是MDBTools。 [disputed] [争议]

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 script that fully demostate pypyodbc's Access support functions. 这是一个Hello World脚本 ,该脚本完全降低了pypyodbc的Access支持功能。

Disclaimer: I'm the developer of pypyodbc. 免责声明:我是pypyodbc的开发人员。

I've used PYODBC to connect succesfully to an MS Access db - on Windows though . 不过,我已经使用PYODBC成功连接到Windows上的MS Access数据库。 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. 安装很容易,用法也很简单,您只需要设置正确的连接字符串(列表中提供了用于MS Access的连接字符串),然后使用示例。

How about pyodbc ? pyodbc怎么样? This SO question demonstrates it's possible to read MS Access using it. 这个SO问题说明可以使用它来读取MS Access。

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 Tools is a set of open source libraries and utilities to facilitate exporting data from MS Access databases (mdb files) without using the Microsoft DLLs. MDB工具是一组开放源代码库和实用程序,用于在不使用Microsoft DLL的情况下方便地从MS Access数据库(mdb文件)导出数据。 Thus non Windows OSs can read the data. 因此,非Windows操作系统可以读取数据。 Or, to put it another way, they are reverse engineering the layout of the MDB file. 或者,换句话说,他们是对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文件,并且可能对该功能的要求不高。

Old question, but I thought I'd post a pypyodbc alternative suggestion for Windows: ADO. 旧问题,但我想我会为Windows发布pypyodbc替代建议: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. 事实证明,通过COM使用现代(相对于老式ODBC)驱动程序来访问Access数据库,Excel电子表格和其他任何东西确实很容易。

Check out the following articles: 查看以下文章:

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. 希望对您有所帮助。

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. 就个人而言,即使经过多次尝试,我也无法使MDB工具(以及相关的ODBC东西(如unixODBC)与Linux上的Python或PHP一起正常使用。 I just tried the instructions in the other answer to this question here and all I got was "Segmentation fault (core dumped)". 我只是在这里尝试了该问题的其他答案中的指示,而我得到的只是“分段错误(核心已转储)”。

However, I did get the UCanAccess JDBC driver to read both .mdb and .accdb files on Linux from either Jython or CPython+JayDeBeApi. 但是,我确实获得了UCanAccess JDBC驱动程序,以从Jython或CPython + JayDeBeApi读取Linux上的.mdb和.accdb文件。 For detailed instructions on how I set it up under Ubuntu 14.04 LTS see my other answer here . 有关如何在Ubuntu 14.04 LTS下进行设置的详细说明,请参见此处的其他答案。

To read an Access database as a pandas dataframe (Windows). 将Access数据库读取为熊猫数据框(Windows)。

This is a very quick and easy solution that I have used successfully for smaller databases. 这是一个非常快速,简便的解决方案,已成功用于较小的数据库。

You can read an Access database by making a permanent link to Excel and saving that file (it takes a couple of clicks), link here: 您可以通过建立指向Excel的永久链接并保存该文件(只需单击几下)来读取Access数据库,请在此处链接:

https://support.office.com/en-gb/article/Connect-an-Access-database-to-your-workbook-a3d6500c-4bec-40ce-8cdf-fb4edb723525 https://support.office.com/zh-CN/article/Connect-an-Access-database-to-your-workbook-a3d6500c-4bec-40ce-8cdf-fb4edb723525

You can then simply read that Excel file as a pandas dataframe. 然后,您可以简单地将该Excel文件作为pandas数据框读取。

So, for example, save the linked Excel file as 'link_to_master.xlsx' in location \\FileStore\\subfolder1\\subfolder. 因此,例如,将链接的Excel文件另存为\\ FileStore \\ subfolder1 \\ subfolder中的“ link_to_master.xlsx”。

Run the following in python: 在python中运行以下命令:

import pandas as pd
import os
os.chdir('\\\\FileStore\\subfolder1\\subfolder') #sets the folder location
df = pd.read_excel('link_to_master.xlsx') # reads the Excel file
df

Consider frequency of the link refresh if you are re-visiting your python script. 如果您重新访问python脚本,请考虑链接刷新的频率。 ie The link between Excel and Access is static. 即Excel和Access之间的链接是静态的。

Most likely, you'll want to use a nice framework like SQLAlchemy to access your data, or at least, I would recommend it. 最有可能的是,您需要使用类似SQLAlchemy的框架来访问数据,或者至少我会推荐它。 Support for Access is "experimental", but I remember using it without too many problems. 对Access的支持是“实验性的”,但我记得使用它没有太多问题。 It itself uses pyodbc under the hood to connect to Access dbs, so it should work from windows, linux, os x and whatnot. 它本身使用引擎盖下的pyodbc连接到Access dbs,因此它应该可以在Windows,Linux,OS X和其他操作系统上运行。

如果您有时间,可以尝试修复和更新此python类,该Python类通过本机COM32客户端API读取MS-Access数据库: Microsoft Access的提取和操作类

The way I connect Python to MS Access under Windows is by using this way: Connect to MS Access with Python . 我在Windows下将Python 连接到MS Access的方法是使用以下方法: 使用Python连接到MS Access Maybe you can find some trouble on Win 7, so I found a solution: Solving a connection between MS Access and Python on Windows 7 也许您会在Win 7上发现一些麻烦,所以我找到了解决方案: 解决Windows 7上MS Access和Python之间的连接

I haven't tried connecting under Linux! 我没有尝试在Linux下连接!

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中也一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 方法不起作用我需要做什么? - Python method not working what i need to do? 我需要跨文件访问变量,我该怎么办? - I need to access variables across files, what should I do? 如果我不需要用户访问令牌,如何使用请求从Python连接到Facebook Graph API? - How to connect to Facebook Graph API from Python using Requests if I do not need user access token? 我需要导入什么才能访问我的模型? - What do I need to import to gain access to my models? 将python可执行文件添加到HTML中,我需要做什么? - What do I need to do for adding python executable to HTML? 蟒蛇; asyncore handle_read;我需要一个单独的线程吗? - python; asyncore handle_read; do I need a seperate thread? 我需要对我的python代码做什么才能让它成为一个模块? - What do I need to do to my python code to get it to be a module? 我在python3中使用协议3腌制文件,现在我需要用python2解开它们,我该怎么办? - I have pickled files using protocol 3 in python3, and now I need to unpickle them with python2, what can I do? 在Google App Engine中使用Soundcloud Python库-我需要移动哪些文件? - Using Soundcloud Python library in Google App Engine - what files do I need to move? 在 Windows XP 中的 cygwin 上使用 python 显示 GBP 符号(磅符号)需要什么编码? - What encoding do I need to display a GBP sign (pound sign) using python on cygwin in Windows XP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM