Skip to content

Instantly share code, notes, and snippets.

@LuanP
Created May 21, 2014 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuanP/68b3cc733506dd3266f7 to your computer and use it in GitHub Desktop.
Save LuanP/68b3cc733506dd3266f7 to your computer and use it in GitHub Desktop.
A script that exports tables from a MDB (Access database) file to SQL files
# Script that exports tables from a MDB (Access database) file to SQL files
# Required:
# mdbtools (apt-get install mdbtools)
# a folder sql_tables in the script directory
### Usage
# create a folder in the current directory called sql_tables
# run this script like the following
# python export_tables.py ACCESS_DATABASE.MDB table1,table2,"My Table 3",other_table
# Done.
import os
import sys
from subprocess import Popen
database = sys.argv[1]
tables = sys.argv[2].split(',')
CURRENT_DIR = os.path.dirname(os.path.abspath('__FILE__'))
SQL_FOLDER_PATH = os.path.join(CURRENT_DIR, 'sql_tables')
for table in tables:
filepath = os.path.join(SQL_FOLDER_PATH, table + '.sql')
print "Loading {} at {}".format(table, filepath)
# exports to a file
cmd = 'mdb-export -D "%B %d, %Y" -q "\'" -I postgres ' \
'{0} "{1}" >> "{2}"'.format(database, table, filepath)
# exports directly to postgres
# cmd = 'mdb-export -D "%B %d, %Y" -q "\'" -I postgres ' \
# '{} "{}" | psql -d sadp2 -U sadp2 -W -h localhost'.format(database,
# table)
process = Popen(cmd, shell=True, executable='/bin/bash')
process.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment