Created
February 26, 2019 16:11
-
-
Save niftycode/1d0154cc272d345f16e217ee3ee0a073 to your computer and use it in GitHub Desktop.
Read an Access Database using Python with the pyodbc module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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