Skip to content

Instantly share code, notes, and snippets.

@turicas
Created May 4, 2012 07:29
Show Gist options
  • Save turicas/2592877 to your computer and use it in GitHub Desktop.
Save turicas/2592877 to your computer and use it in GitHub Desktop.
Convert a MDB (Access) file to SQL
#!/bin/bash
# Convert a MDB file (Access) to SQL
# Needs mdbtools[http://mdbtools.sourceforge.net/]
# run 'aptitude install mdbtools' on Debian/Ubuntu
# Created by Álvaro Justen <https://github.com/turicas>
# License: GPLv2
mdb=$1
sql=$2
if [ -z "$2" ]; then
echo 'This script convert a MDB file to SQL file. You need to specify the name of both'
echo "Usage: $0 <mdb_file> <sql_file>"
exit 1
fi
if [ -z "$(which mdb-tables)" ]; then
echo 'You need mdbtools installed.'
echo 'Learn more at http://mdbtools.sourceforge.net/'
echo 'If you use Debian/Ubuntu, just execute:'
echo ' sudo aptitude install mdbtools'
exit 2
fi
mdb-schema $mdb > $sql
sed -i 's/Long Integer/INT(11)/g; s/Text /VARCHAR/g' $sql
for table in $(mdb-tables $mdb); do
mdb-export -I -R ';' $mdb $table >> $sql
done
sed -i '/^-\{2,\}/d; s/DROP TABLE /DROP TABLE IF EXISTS /' $sql
@kurahaupo
Copy link

The same thing with all error checking removed:

#!/bin/bash

mdb=$1
output=${2:--}
dialect=${3:-mysql}

[[ $output != - ]] && exec >"$output"

mdb-schema "$mdb" "$dialect"

mdb-tables -1 "$mdb" |
while read -r table
do
    mdb-export -I "$dialect" -q\' -boctal -D%F "$mdb" "$table"
done

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