Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Access library for python [duplicate]

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?

like image 800
Vicky Avatar asked Jun 26 '09 06:06

Vicky


People also ask

Can you use Python in MS Access?

TL;DR – You need 32-bit Python for 32-bit Access, or 64-bit Python for 64-bit Access. One thing to note upfront, if you have 64-bit MS Access, you'll want to use the 64-bit Python for this exercise. Mixing up a 64-bit Python with 32-bit Access will throw an error when you try to connect.

Which DB is best for Python?

PostgreSQL database PostgreSQL is the recommended relational database for working with Python web applications.


2 Answers

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.

like image 53
stephan Avatar answered Oct 03 '22 12:10

stephan


I don't think win32 is hard. Try use its odbc module. Example of code working with 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 similar for every db driver I used in Python and Jython (I work with PostgreSQL, Oracle and Informix).

like image 27
Michał Niklas Avatar answered Oct 03 '22 11:10

Michał Niklas