Skip to content
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ The USDA has a database of nutritional information for many foods available at:

http://www.ars.usda.gov/Services/docs.htm?docid=8964

By default it's contained in flat files. This project includes scripts to generate appropriate schema and import them into a SQLite database. I've also hand generated a subset of the nutrients that includes commonly-familiar nutrients vs. the very extensive list provided by the USDA.
By default it's contained in flat files. This project includes scripts to generate appropriate schema and import them into a SQLite database. @alyssaq also hand generated a subset of the nutrients that includes commonly-familiar nutrients vs. the very extensive list provided by the USDA.

You will need to first download the files and unzip them into the `data` directory. Files for SR28 are included.
You will need to first download the files and unzip them into the `data` directory. Files for SRLegacy are included.

Run create_db.sh with the name of output database:
```sh
$ ./create_db.sh usda.sql3
```
On my system it takes about 10 seconds to populate the db.
It takes about 10 seconds to populate the db.

Some example scripts to query the db are in the `example` folder. Sample usage:
```sh
$ python example/nutrition.py 01001
$ python example/search.py salmon
```
## SQLlite browser
I recommend using the open source [sqlitebrowser](http://sqlitebrowser.org/) to view the `usda.sql3` database.
It's recommended to use the open source [sqlitebrowser](http://sqlitebrowser.org/) to view the `usda.sql3` database.
![sqllitebrowser_screenshot](
https://github.com/sqlitebrowser/sqlitebrowser/raw/master/images/sqlitebrowser.png)

Expand All @@ -43,24 +43,24 @@ You can also upload the created `*.sql3` file to https://sqliteonline.com/, savi
Every food's serving size, if it has one

```sql
SELECT
SELECT
food.id,
food.long_desc,
gm_weight
FROM food
FROM food
INNER JOIN weight ON weight.food_id=food.id
where weight.description=='serving';
```

Nutrient amounts per 100g of food

```sql
SELECT
SELECT
food.id,
nutrient.id,
nutrition.amount
FROM nutrition
INNER JOIN food ON nutrition.food_id=food.id
FROM nutrition
INNER JOIN food ON nutrition.food_id=food.id
INNER JOIN nutrient ON nutrition.nutrient_id=nutrient.id;
```

Expand All @@ -83,4 +83,4 @@ console.log(JSON.stringify(b)); //note this add " characters to the beginning an
```

## Attribution
Adapted from <https://github.com/czarandy/usda> for scripts to run on Mac OS X with SR28 release.
Adapted from <https://github.com/czarandy/usda> for scripts to run on Mac OS X with SR28 release and from https://github.com/alyssaq/usda-sqlite to add missing tables.
21 changes: 21 additions & 0 deletions create_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ echo "Loading nutrient..."
sqlite3 $1 < load_nutrient.sql
sqlite3 $1 < load_common_nutrient.sql

echo "Loading derivation code..."
sqlite3 $1 < load_derivation_code.sql

echo "Loading factor..."
sqlite3 $1 < load_factor.sql

echo "Loading factor link..."
sqlite3 $1 < load_factor_link.sql

echo "Loading data source..."
sqlite3 $1 < load_data_source.sql

echo "Loading data source link..."
sqlite3 $1 < load_data_source_link.sql

echo "Loading footnote..."
sqlite3 $1 < load_footnote.sql

echo "Loading source code..."
sqlite3 $1 < load_source_code.sql

echo "Loading nutrition..."
sqlite3 $1 < load_nutrition.sql

Expand Down
1,288 changes: 606 additions & 682 deletions data/DATA_SRC.txt
100755 → 100644

Large diffs are not rendered by default.

Loading