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 r
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.
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.
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
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.
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.
How about pyodbc? This SO question demonstrates it's possible to read MS Access using it.