Skip to content

Instantly share code, notes, and snippets.

@wsdookadr
Last active March 12, 2021 23:46
Show Gist options
  • Select an option

  • Save wsdookadr/aa1d0dea01e99f65875097e384a753a4 to your computer and use it in GitHub Desktop.

Select an option

Save wsdookadr/aa1d0dea01e99f65875097e384a753a4 to your computer and use it in GitHub Desktop.
Looping over rows in an SQLite database using Bash
  • Description

This is a code sample showing how to query an SQLite database using Bash and loop through query results.

To run this sample:

make gen_data
make build_db
make query
  • Contact

If you need help with SQLite, my company provides a variety of database-related services.

gen_data:
echo "1 2 3" > data.tsv
echo "4 5 6" >> data.tsv
echo "7 8 9" >> data.tsv
build_db:
sqlite3 data.db < schema.sql
query:
./query.sh
#!/bin/bash
for row_str in $(sqlite3 data.db 'select * from t1;'); do
IFS='|' read -r -a COLS <<< "$row_str"
echo "${COLS[2]}"
done
drop table if exists t1;
create table t1 (
a int,
b int,
c int
);
.mode ascii
.separator " " "\n"
.import data.tsv t1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment