Skip to content

Instantly share code, notes, and snippets.

@mywarr
Last active April 18, 2024 19:14
Show Gist options
  • Save mywarr/9908044 to your computer and use it in GitHub Desktop.
Save mywarr/9908044 to your computer and use it in GitHub Desktop.
use mdbtools to convert .mdb to .sqlite and .csv
#!/usr/bin/env python
#
# AccessDump.py
# A simple script to dump the contents of a Microsoft Access Database.
# It depends upon the mdbtools suite:
# http://sourceforge.net/projects/mdbtools/
import sys, subprocess, os
DATABASE = sys.argv[1]
# Dump the schema for the DB
subprocess.call(["mdb-schema", DATABASE, "mysql"])
# Get the list of table names with "mdb-tables"
table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE],
stdout=subprocess.PIPE).communicate()[0]
tables = table_names.splitlines()
print "BEGIN;" # start a transaction, speeds things up when importing
sys.stdout.flush()
# Dump each table as a CSV file using "mdb-export",
# converting " " in table names to "_" for the CSV filenames.
for table in tables:
if table != '':
subprocess.call(["mdb-export", "-I", "mysql", DATABASE, table])
print "COMMIT;" # end the transaction
sys.stdout.flush()
#!/usr/bin/env sh
# prerequisite https://github.com/brianb/mdbtools
#
# if your mdb is encoded in certain character set
# do something like this
# export MDB_ICONV="Big-5"
# export MDB_JET3_CHARSET="Big-5"
#
# under zsh
# for f (**/*.mdb) {~/dump.sh $f}
#
python ~/AccessDump.py $1 | sqlite3 $1.sqlite
for x in `mdb-tables -1 $1`; do mdb-export $1 $x >> $1.$x.csv ; done
@s-nt-s
Copy link

s-nt-s commented Jul 9, 2021

Why you use mysql instead of sqlite as parameter of mdb-schema and mdb-export?

@renegarcia
Copy link

This seems to work with python3 and sqlite

#!/usr/bin/env python3
#
# AccessDump.py
# A simple script to dump the contents of a Microsoft Access Database.
# It depends upon the mdbtools suite:
#   http://sourceforge.net/projects/mdbtools/

import sys, subprocess, os

DATABASE = sys.argv[1]

# Dump the schema for the DB
subprocess.call(["mdb-schema", DATABASE, "sqlite"])

# Get the list of table names with "mdb-tables"
table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE],
                               stdout=subprocess.PIPE).communicate()[0]
tables = table_names.splitlines()

print("BEGIN;") # start a transaction, speeds things up when importing
sys.stdout.flush()

# Dump each table as a CSV file using "mdb-export",
# converting " " in table names to "_" for the CSV filenames.
for table in tables:
    if table != '':
        subprocess.call(["mdb-export", "-I", "sqlite", DATABASE, table])

print("COMMIT;") # end the transaction
sys.stdout.flush()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment