diff --git a/bin/sqlite3ncsv b/bin/sqlite3ncsv new file mode 120000 index 0000000..a3f13db --- /dev/null +++ b/bin/sqlite3ncsv @@ -0,0 +1 @@ +../sqlite3ncsv \ No newline at end of file diff --git a/sqlite3ncsv b/sqlite3ncsv new file mode 100755 index 0000000..422e484 --- /dev/null +++ b/sqlite3ncsv @@ -0,0 +1,23 @@ +#!/bin/bash +[[ -f "$1" ]] || { + echo 'Sqlite3NCSV: view sqlite3 DB with ncsv. + first argument: sqlite3 file + second, optional argument: table name. If not given, first in schema used. + If the second argument contains spaces, it is assumed to be a SELECT query. + ' + exit +} + +[[ -z "$2" ]] && { + table=$( sqlite3 "$1" "SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite%' LIMIT 1;" ) +} || { + table="$2" +} +[[ "$table" = "${table% *}" ]] && { + query="SELECT * FROM $table;" +} || { + query="$table" +} + +sqlite3 -header -separator ' ' -nullvalue NA "$1" "$query" | ncsv +