MS Access library for python

Vicky picture Vicky · Jun 26, 2009 · Viewed 94.1k times · Source

Is there a library for using MS Access database in python? The win32 module is not as easy as the MySQL library. Is there a simpler way to use MS Access with Python?

Answer

stephan picture stephan · Jun 26, 2009

Depending on what you want to do, pyodbc might be what you are looking for.

import pyodbc

def mdb_connect(db_file, user='admin', password = '', old_driver=False):
    driver_ver = '*.mdb'
    if not old_driver:
        driver_ver += ', *.accdb'

    odbc_conn_str = ('DRIVER={Microsoft Access Driver (%s)}'
                     ';DBQ=%s;UID=%s;PWD=%s' %
                     (driver_ver, db_file, user, password))

    return pyodbc.connect(odbc_conn_str)

conn = mdb_connect(r'''C:\x.mdb''')  # only absolute paths!

Note: you may download the freely-redistributable new-driver, if you don't have MSOffice installed.