Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SR26 #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions Makefile

This file was deleted.

10 changes: 5 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ information produced by the USDA.
You must first create a new user and database, make sure that these are
referred to in createdb.sh.

Then make sure you have a recent copy of mdb-tools. These tools are used to
Then make sure you have a recent copy of mdbtools. These tools are used to
convert the sr22 data from access format into SQL.

Edit create_db.sh to reflect your local MySQL installation, user, password, and
database.
Edit createdb.sh to reflect your local MySQL installation: user, password, and
database. Also set the version number of the SR database you wish to use.

Then:

make sr22
./createdb.sh

This should insert all the data into your mysql database!
This should download and insert all the data into your mysql database!
53 changes: 35 additions & 18 deletions createdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,52 @@
# (http://www.ars.usda.gov/Services/docs.htm?docid=18879)
# draws heavily on http://blog.moybella.net/2007/03/10/converting-microsoft-access-mdb-into-csv-or-mysql-in-linux/

SR22=data/sr22.mdb # data from the USDA in m$ access format
VER=26
MYSQLUSER=youruser
MYSQLPASS=yourpass
MYSQLDB=ndb
MYSQLCONN="mysql -u $MYSQLUSER --password=$MYSQLPASS $MYSQLDB"
TABLES=`mdb-tables $SR22` # a list of tables in our input

# create the database if it doesn't exist

echo creating database $MYSQLDB
echo "CREATE DATABASE IF NOT EXISTS $MYSQLDB;" | $MYSQLCONN || exit 1
MDB=data/sr${VER}.mdb # data from the USDA in m$ access format

# If we already ahve MDB file, do nothing.
if [[ -f data/sr${VER}.mdb ]]
then
# do nothing
true

# If we have the ZIP file, extract MDB.
elif [[ -f data/sr${VER}db.zip ]]
then
unzip data/sr${VER}db.zip sr${VER}.mdb
mv sr${VER}.mdb data/

# Otherwise, download ZIP and extract MDB.
else
wget http://www.ars.usda.gov/SP2UserFiles/Place/12354500/Data/SR${VER}/dnload/sr${VER}db.zip || exit 1
mv sr${VER}db.zip data/
unzip data/sr${VER}db.zip sr${VER}.mdb
mv sr${VER}.mdb data/
fi

# put the schema into the database

echo creating schema in $MYSQLDB
#mdb-schema $SR22 | $MYSQLCONN
# The above should work, but the schema requires manual tweaking.
# The schema that mdb-schema produces from the sr22 access database does not
# match mysql's spec.
# Instead we use this hand-edited schema:
$MYSQLCONN <sr22.sql

# If custom schema is available, use it. If not, use what is available.
if [[ -f sr${VER}.sql ]]
then
$MYSQLCONN <sr${VER}.sql
else
mdb-schema --drop-table $MDB mysql | $MYSQLCONN
fi

TABLES=`mdb-tables $MDB` # a list of tables in our input
echo adding tables to $MYSQLDB

for table in $TABLES
do
echo adding $table
mdb-export -H $SR22 $table > $table.sql
mysqlimport -L -u $MYSQLUSER --password=$MYSQLPASS --fields-terminated-by="," --fields-enclosed-by="\"" $MYSQLDB $table.sql
rm $table.sql
echo adding $table
mdb-export -I mysql $MDB $table | sed '1i BEGIN;' | sed '$ a COMMIT;' | $MYSQLCONN
done

echo done

2 changes: 1 addition & 1 deletion sr22.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ CREATE TABLE ABBREV
Beta_Carot DOUBLE,
Beta_Crypt DOUBLE,
Lycopene DOUBLE,
Lut_Zea DOUBLE, -- this was Lut+Zea
`Lut+Zea` DOUBLE, -- this was Lut+Zea
Vit_E DOUBLE,
Vit_D_mcg DOUBLE,
ViVit_D_IU DOUBLE,
Expand Down
Loading