Last active
October 26, 2023 08:46
-
-
Save liuxd/de880bcff04becd33beefdf426383e28 to your computer and use it in GitHub Desktop.
[Convert .mdb to .sqlite in python on Mac] #MS Access
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
# It requires mdbtools. | |
from subprocess import Popen, PIPE | |
import subprocess | |
import sqlite3 | |
def run_cmd(cmd): | |
p = Popen(cmd, stdout=PIPE, stderr=PIPE) | |
output, error = p.communicate() | |
output = str(output, 'utf-8') | |
return output | |
def convert_mdb_to_sqlite(mdb, sqlite_file): | |
sqlite_db = sqlite3.connect(sqlite_file) | |
schema = run_cmd(["mdb-schema", mdb, "sqlite"]) | |
for table_create in schema.split(';'): | |
sqlite_db.execute(table_create) | |
table_names = subprocess.Popen(["mdb-tables", "-1", mdb], stdout=subprocess.PIPE).communicate()[0] | |
tables = table_names.splitlines() | |
for table in tables: | |
if table != '': | |
records = run_cmd(["mdb-export", "-I", "sqlite", mdb, table]) | |
for record in records.split("\n"): | |
sqlite_db.execute(record) | |
sqlite_db.commit() | |
sqlite_db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment