#!/usr/bin/env bash # https://perlmonks.org/?node_id=11150232 # SQLite llil import script. # Usage: bash llil_sql_imp2 [-reset] hash2.db big1.txt big2.txt big3.txt # SQL function make_script () { read -r -d '' var << 'EOF' -- Set SQLite pragmas. -- .output /dev/null PRAGMA cache_size = 10000; PRAGMA page_size = 4096; PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; .output -- Create the kv_store table. -- CREATE TABLE IF NOT EXISTS "kv_store" ( "name" TEXT NOT NULL, "value" INTEGER DEFAULT 0 ); -- Import CSV/TXT file(s). -- .mode csv .headers off .separator ROW "\n" .separator "\t" EOF echo "$var" } # RUN if [[ "$1" == "-reset" ]]; then DB="$2"; shift; shift rm -fr "$DB" "$DB"-journal "$DB"-shm "$DB"-wal else DB="$1"; shift fi if [[ ! "$DB" =~ .db$ ]]; then echo "usage: bash $0 [ -reset ] hash2.db [ in1.txt ... ]" >&2 exit 1 fi SQLCMND=$( command -v sqlite3 ) if [[ -z "$SQLCMND" ]]; then echo "sqlite3: command not found" >&2 exit 1 fi SQLSCRIPT="$( make_script )" NL=$'\n' for infile in "$@"; do if [[ ! -r "$infile" ]]; then echo "$infile: cannot open '$infile', skipping..." >&2 continue fi SQLSCRIPT+="${NL}.print Importing ${infile}" SQLSCRIPT+="${NL}.import '${infile}' kv_store" done exec "$SQLCMND" "$DB" < <(echo "$SQLSCRIPT")