PyODBC

From EJP Documentation
Jump to navigation Jump to search

What is it?

PyODBC is a python module that lets you load various database sources, namely MS Access, into your python program

Must install the 32-bit python 2.7 version


Project Website https://code.google.com/p/pyodbc/
License MIT License
Installer https://code.google.com/p/pyodbc/downloads/detail?name=pyodbc-3.0.7.win32-py2.7.exe&can=2&q=


Installation

  1. Just run the installer, it should detect our 2.7 install


Example code

 import pyodbc
 ACCESS_DATABASE_FILE = "C:\\Users\\aantony\\Documents\\Database1.accdb"
 ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % ACCESS_DATABASE_FILE
 
 conn = pyodbc.connect(ODBC_CONN_STR)
 
 query = "select title, author from books"
 
 result = conn.execute(query)
 
 rows = result.fetchall()
 for row in rows:
     print "Title: %s, Author: %s" % (row.title , row.author)
 OUTPUT:
 
 Title: Invent With Python, Author: Al Swiegart
 Title: Making Gamed with Python, Author: Al Swiegart