Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.