Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active October 26, 2023 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuxd/de880bcff04becd33beefdf426383e28 to your computer and use it in GitHub Desktop.
Save liuxd/de880bcff04becd33beefdf426383e28 to your computer and use it in GitHub Desktop.
[Convert .mdb to .sqlite in python on Mac] #MS Access
# 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