Can someone point me in the right direction on how to open a .mdb file in python? I normally like including some code to start off a discussion, but I don't know where to start. I work with mysql a fair bit with python. I was wondering if there is a way to work with .mdb files in a similar way?
6 Answers
Below is some code I wrote for another SO question.
It requires the 3rd-party pyodbc module.
This very simple example will connect to a table and export the results to a file.
Feel free to expand upon your question with any more specific needs you might have.
import csv, pyodbc
# set up some constants
MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'
# connect to db
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
# run a query and get the results
SQL = 'SELECT * FROM mytable;' # your query goes here
rows = cur.execute(SQL).fetchall()
cur.close()
con.close()
# you could change the mode from 'w' to 'a' (append) for any subsequent queries
with open('mytable.csv', 'w') as fou:
csv_writer = csv.writer(fou) # default field-delimiter is ","
csv_writer.writerows(rows)

- 163,903
- 24
- 228
- 223
-
5This doesn't seem to work from Linux, since there's no driver included by default with pyodbc for reading Access. "Data source name not found, and no default driver specified (0) (SQLDriverConnect)" – Cerin Jun 17 '14 at 18:13
-
@Cerin: thanks for the update. Since the question didn't specify "from Linux" I did not address that concern. Best regards. – mechanical_meat Jun 17 '14 at 21:48
-
3@bernie is there a way to do this in linux? – NumenorForLife Jun 20 '15 at 19:23
-
2@jsc123 For a solution that works in Linux, see [my answer](http://stackoverflow.com/a/33662056/2144390). – Gord Thompson Nov 11 '15 at 23:53
-
The 'with open' line here didn't work for me (using Python 3.51). I changed it to 'with open('mytable.csv', 'w', newline='') as fou:' and all was ok :) – Neil Billingham Apr 11 '16 at 11:02
-
@mechanical_meat i am getting the following error in writing the csv files. csv_writer.writerows(rows) TypeError: a bytes-like object is required, not 'str' Line 22 – Vignesh Jun 18 '20 at 10:51
-
1@VigneshRajendran: change the mode from `'wb'` to `'w'`. Answer edited. – mechanical_meat Jun 18 '20 at 11:28
-
this works till half of the database after that this error comes. UnicodeEncodeError: 'charmap' codec can't encode character '\x90' in position 707: character maps to
– Vignesh Jun 18 '20 at 12:01 -
@mechanical_meat is this have the read-only option? because I am accessing a network shared file, while reading it it prevents other users from opening the file. – Vignesh Jul 30 '20 at 11:51
-
4In more recent Access versions it should be `Microsoft Access Driver (*.mdb, *.accdb)`. – Robin Dinse Oct 14 '20 at 15:43
-
If you are getting the "Data source name not found, and no default driver specified (0) (SQLDriverConnect)" error, make sure you have the MS Access driver installed on your machine: https://www.microsoft.com/en-us/download/details.aspx?id=13255. The x86 version worked for me. Make sure to restart your python environment as well. – cgage1 Jan 12 '22 at 21:39
There's the meza library by Reuben Cummings which can read Microsoft Access databases through mdbtools.
Installation
# The mdbtools package for Python deals with MongoDB, not MS Access.
# So install the package through `apt` if you're on Debian/Ubuntu
$ sudo apt install mdbtools
$ pip install meza
Usage
>>> from meza import io
>>> records = io.read('database.mdb') # only file path, no file objects
>>> print(next(records))
Table1
Table2
…

- 13,194
- 14
- 74
- 96

- 5,148
- 4
- 47
- 74
-
2works fine with Linux (tested in CenOs 7 and Ubuntu with python 2.7), very easy way to read data from .mdb files. Thanks ;-) – serfer2 May 19 '17 at 13:39
-
-
1@karnataka It uses the MIT license, so yes. (https://github.com/reubano/meza/blob/master/LICENSE) – jnns Oct 01 '20 at 09:46
-
I have a large MDB file and its giving error read: Is a directory Couldn't read the first page. Couldn't open database. – karnataka Oct 05 '20 at 05:55
-
@karnataka You should create a separate question in which you lay out your steps that produce the error you mention. – jnns Oct 05 '20 at 13:14
-
I have tried it for `lmdb` file but I am getting `KeyError: 'lmdb'` error – alper Jan 01 '21 at 21:03
-
@alper please create a separate question with a detailed description of what you are doing and the error message you get. – jnns Jan 03 '21 at 07:53
This looks similar to a previous question:
- What do I need to read Microsoft Access databases using Python?
- http://code.activestate.com/recipes/528868-extraction-and-manipulation-class-for-microsoft-ac/
Answer there should be useful.
For a solution that works on any platform that can run Java, consider using Jython or JayDeBeApi along with the UCanAccess JDBC driver. For details, see the related question
Read an Access database in Python on non-Windows platform (Linux or Mac)

- 116,920
- 32
- 215
- 418
In addition to bernie's response, I would add that it is possible to recover the schema of the database. The code below lists the tables (b[2] contains the name of the table).
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
tables = list(cur.tables())
print 'tables'
for b in tables:
print b
The code below lists all the columns from all the tables:
colDesc = list(cur.columns())

- 11
- 2
This code will convert all the tables to CSV.
Happy Coding
for tbl in mdb.list_tables("file_name.MDB"):
df = mdb.read_table("file_name.MDB", tbl)
df.to_csv(tbl+'.csv')

- 9,869
- 6
- 42
- 59

- 776
- 6
- 11
-
2
-
its basically the Microsoft database file and after importing data in it you can convert it to the data frame using the above method – Malik Mussabeheen Noor Nov 06 '20 at 09:50
-
2without an `import mdb` or some line like `mdb = ...`, this code snippet is very unclear... – Fee Dec 24 '20 at 14:33
-
most probably forgot the library : 'pandas_access'
import pandas_access as mdb
more information : # https://pypi.org/project/pandas_access/ - plugin # https://pydigger.com/pypi/pandas_access - *** – kris Mar 25 '21 at 12:58