Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@MikiDi

This comment has been minimized.

Copy link

commented Oct 24, 2015

Using the current version of mdbtools the mdb-export -I -R ';' $mdb $table >> $sql command fails as the -I-option needs an argument. I also think the -R option seems to be no longer needed (; is automatically appended).

@douglasfernandes

This comment has been minimized.

Copy link

commented Oct 10, 2017

line 28:
mdb-export -I postgres $mdb "$table" >> $sql
postgres, mysql are ok.

@franco999

This comment has been minimized.

Copy link

commented Jun 7, 2018

It works correctly for me as @douglasfernandes says.
line 28: mdb-export -I mysql $mdb "$table" >> $sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.