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

Question:

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.

Asked By: Georg Schölly

||

Answers:

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

Answered By: stuartd

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.

Answered By: Nico

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.

Answered By: apenwarr

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.

Answered By: Alex Boschmans

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.

Answered By: Tony Toews

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.

Answered By: pypyodbc

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:

Answered By: Erik Knowles

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.

Answered By: Menda

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

Answered By: ankostis

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.

Answered By: Gord Thompson

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!

Answered By: combuilder

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

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.

Answered By: Tim0th1