Last active
April 18, 2024 19:14
-
-
Save mywarr/9908044 to your computer and use it in GitHub Desktop.
use mdbtools to convert .mdb to .sqlite and .csv
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 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() |
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 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 |
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
Why you use
mysql
instead ofsqlite
as parameter ofmdb-schema
andmdb-export
?