Skip to content

Instantly share code, notes, and snippets.

@martjanz
Last active October 28, 2015 04:40

Revisions

  1. martjanz revised this gist Oct 28, 2015. No changes.
  2. martjanz renamed this gist Oct 28, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. martjanz created this gist Sep 26, 2015.
    2 changes: 2 additions & 0 deletions mb2sqlite.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    MDB_JET3_CHARSET="cp1252"
    python mdb2sql.py access.mdb | sqlite3 db.sqlite
    30 changes: 30 additions & 0 deletions mdb2sql.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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()