[ACCEPTED]-MS Access library for python-ms-access

Accepted answer
Score: 43

Depending on what you want to do, pyodbc might 3 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 2 the freely-redistributable new-driver, if you don't 1 have MSOffice installed.

Score: 13

I don't think win32 is hard. Try use its 5 odbc module. Example of code working with 4 ODBC and PostgreSQL database:

import odbc

def get_pg_ver(db_alias):
    connection = odbc.odbc(db_alias)
    try:
        cursor = connection.cursor()
        cursor.execute('SELECT version()')
        for row in cursor.fetchall():
            print row[0]
    finally:
        connection.close()

get_pg_ver('odbc_name/user/passwd')

This is very 3 similar for every db driver I used in Python 2 and Jython (I work with PostgreSQL, Oracle 1 and Informix).

Score: 8

You can use pypyodbc to easily create an empty Access 8 MDB file on win32 platform, and also compact 7 existing Access MDB files.

It can be as easy 6 as:

import pypyodbc
pypyodbc.win_create_mdb( "D:\\Your_MDB_file_path.mdb" )

More over, as an dbi 2.0 ODBC library, pypyodbc 5 is highly compatible with pyodbc, you can 4 do SQL database queries like SELECT, INSERT, UPDATE 3 with the library.

Here is the full Tutorial about 2 pypyodbc's Access support.

Disclaimer: I'm 1 the developer of pypyodbc.

Score: 4

I had some recent success using pywin32's 2 adodbapi module.

The following snippet 1 was taken from this website:

import adodbapi

database = "db1.mdb"
constr = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s'  % database
tablename = "address"

# connect to the database
conn = adodbapi.connect(constr)

# create a cursor
cur = conn.cursor()

# extract all the data
sql = "select * from %s" % tablename
cur.execute(sql)

# show the result
result = cur.fetchall()
for item in result:
    print item

# close the cursor and connection
cur.close()
conn.close()

More Related questions