Skip to content

Instantly share code, notes, and snippets.

@ip413
Forked from mywarr/AccessDump.py
Created May 30, 2017 09:14
Show Gist options
  • Save ip413/ca507046401db7f073ad03d36dfbcb3f to your computer and use it in GitHub Desktop.
Save ip413/ca507046401db7f073ad03d36dfbcb3f 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment