Skip to content

Instantly share code, notes, and snippets.

@niftycode
Created February 26, 2019 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftycode/1d0154cc272d345f16e217ee3ee0a073 to your computer and use it in GitHub Desktop.
Save niftycode/1d0154cc272d345f16e217ee3ee0a073 to your computer and use it in GitHub Desktop.
Read an Access Database using Python with the pyodbc module.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Read an Access Database using pyodbc
Version: 1.0
Python 3.7
Date created: 24.02.2019
"""
import pyodbc
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\Users\bodo\Documents\Projekte.accdb;'
)
# Connect to database
connection = pyodbc.connect(conn_str)
# 'Open' database
cursor = connection.cursor()
# Show tables
for table_info in cursor.tables(tableType='TABLE'):
print(table_info.table_name)
# Fetch the data (from the 'Projekte' table)
cursor.execute('SELECT * FROM %s' % ('Projekte'))
db_data = cursor.fetchall()
# Show the fetched data
for row in db_data:
print (row)
# Close the connection
cursor.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment