Skip to content

Instantly share code, notes, and snippets.

@amirkdv amirkdv/mdbexport.sh
Last active Sep 3, 2016

Embed
What would you like to do?
Proof of concept usage of mdbtools to export MDB data
# slightly modified version of https://gist.github.com/turicas/2592877
# do not trust the following; use ODBC to read from mdb files instead
# install 'mdbtools' which provides mdb-schema, mdb-tables and mdb-export
# note $mdb_file and $dest_backend
set -e
dest_backend=mysql
mdb_file=/path/to/file.mdb
echo "-- converted .mdb database; powered by MDBTools" > convert.sql
mdb-schema "$mdb_file" "$dest_backend" \
| sed "s/Int8/INTEGER(8)/" \
| sed "s/Int4/INTEGER(4)/" \
| sed "s/Float8/FLOAT(8)/" \
| sed "s/Float4/FLOAT(4)/" \
| sed "s/Bool/BOOLEAN/" \
| sed "s/Char /VARCHAR/" \
| sed "s/DROP TABLE/DROP TABLE IF EXISTS/" \
| grep -v "^--" \
>> convert.sql
echo "BEGIN;" >> convert.sql
# Export each table and replace nan and inf with NULL
for table in "$( mdb-tables "$mdb_file" | sed 's/ *$//' )"; do
mdb-export -I $dest_backend "$mdb_file" "$table" |sed -e 's/)$/)\;/'\
| sed "s/-inf/NULL/mg" \
| sed "s/inf/NULL/mg" \
| sed "s/-nan/NULL/mg" \
| sed "s/nan/NULL/mg" \
>> convert.sql
done
echo "COMMIT;" >> convert.sql
@shawe

This comment has been minimized.

Copy link

commented Sep 3, 2016

Error: Table Table1 Table2 Table3 does not exist in this database.

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.