-
-
Save pcannon67/9217394fc7166fe25e86c2d452ef8f52 to your computer and use it in GitHub Desktop.
A modified version of the "Converting MS Access mdb files to sqlite" script posted at: http://pnenp.wordpress.com/2011/02/10/converting-ms-access-mdb-files-to-sqlite-mdb2sqlite/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Inspired by: | |
# http://nialldonegan.me/2007/03/10/converting-microsoft-access-mdb-into-csv-or-mysql-in-linux/ | |
# http://cltb.ojuba.org/en/articles/mdb2sqlite.html | |
# Dave's Modifications: | |
# Line 25: Added code to remove COMMENT and SET statements. | |
# Lines 28 to 37: Added code to handle primary keys. | |
# Line 51: Added "postgres" to mdb-export command. | |
# My Modifications: | |
# Removed null/inf sed code as it would match any element starting with that pattern | |
# Updated table export to handle table names with spaces | |
# Use temporary files for sql statements to ease debugging if something goes wrong | |
# Export schema from mdb: | |
mdb-schema $1 postgres \ | |
| 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 -Ev "^--|COMMENT|SET" \ | |
> create.sql | |
# Remove PRIMARY KEY Statement and add it to the TABLE definition | |
grep "CONSTRAINT" create.sql | while read k; do | |
TABLEIS=`echo $k | cut -f2 -d '"'` | |
COLUMNIS=`echo $k | cut -f6 -d '"'` | |
LINEOFCREATE=`grep -n 'CREATE TABLE "'"${TABLEIS}"'"' create.sql | cut -f1 -d:` | |
LINEOFCOLUMN=`grep -A 100 'CREATE TABLE "'"${TABLEIS}"'"' create.sql | grep -n '"'"${COLUMNIS}"'"' | head -1 | cut -f1 -d:` | |
ACTUALLINE=$((LINEOFCREATE+LINEOFCOLUMN-1)) | |
sed -i -e "${ACTUALLINE}s/,/ PRIMARY KEY,/" create.sql | |
sed -i -e "/^.*${k}.*$/d" create.sql | |
done | |
# Import schema to sqlite3 | |
sqlite3 $2<create.sql | |
# Delete old import data (adding to exising file later) | |
# Vast speed improvement with BEGIN..COMMIT | |
echo "BEGIN;">import-data.sql | |
# Export each table separating with a comma to support table names with spaces | |
# Get the old separator | |
OLD_IFS=${IFS} | |
# Set the comma | |
IFS=$',' | |
# Output table names with a comma delimiter | |
for table in `mdb-tables -d"," $1` | |
do | |
mdb-export -I postgres $1 $table |sed -e 's/)$/)\;/'\ | |
>>import-data.sql | |
done | |
# Reset | |
IFS=${OLD_IFS} | |
echo "COMMIT;">>import-data.sql | |
# Import data to sqlite3 | |
sqlite3 $2<import-data.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on a macOS with MacPorts version of mdbtools.