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

邮差的信 提交于 2019-11-26 12:18:57

问题


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

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

I only require read access.


回答1:


I've used PYODBC to 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.




回答2:


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

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

To create an Access mdb file:

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.

Disclaimer: I'm the developer of pypyodbc.




回答3:


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




回答4:


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 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. Thus non Windows OSs can read the data. Or, to put it another way, they are reverse engineering the layout of the MDB file.

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.




回答5:


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.

Check out the following articles:

  • http://www.mayukhbose.com/python/ado/index.php
  • http://www.markcarter.me.uk/computing/python/ado.html
  • http://www.ecp.cc/ado_examples.shtml



回答6:


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

Install 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.




回答7:


To read an Access database as a pandas dataframe (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:

https://support.office.com/en-gb/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.

So, for example, save the linked Excel file as 'link_to_master.xlsx' in location \FileStore\subfolder1\subfolder.

Run the following in 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. i.e. The link between Excel and Access is static.




回答8:


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 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. For detailed instructions on how I set it up under Ubuntu 14.04 LTS see my other answer here.




回答9:


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




回答10:


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.

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




回答11:


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




回答12:


The way I connect Python to MS Access under Windows is by using this way: Connect to MS Access with Python. 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

I haven't tried connecting under Linux!



来源:https://stackoverflow.com/questions/853370/what-do-i-need-to-read-microsoft-access-databases-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!