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.
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.
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 off you go with the examples.
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.
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 off you go with the examples.
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.
man mdb-
and then tab to get the auto-complete list; and everyone can see the txt files in this Github repository. –
Vanillic How about pyodbc? This SO question demonstrates it's possible to read MS Access using it.
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.
libmdbodbc1
is now replaced with odbc-mdbtool
. At least thats what I got working on Ubuntu 18.04. –
Donets odbc-mdbtools
(s at end) –
Tibold brew edit mdbtools
and futzing around to install unicodbc, this test code worked for me (after updating the last like to print(row)
) –
Prieto 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:
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.
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.
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:
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 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.
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
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!
© 2022 - 2024 — McMap. All rights reserved.