42 lines
971 B
Bash
Executable File
42 lines
971 B
Bash
Executable File
#!/bin/bash
|
|
help() {
|
|
echo 'sqlite3tabview: view sqlite3 DB with tabview.
|
|
Usage: sqlite3tabview [FILE] [TABLE/QUERY/-l]
|
|
|
|
first argument: sqlite3 file
|
|
second, optional argument:
|
|
TABLE: If not given, first in schema used.
|
|
QUERY: If the second argument contains spaces, it is assumed to be a query.
|
|
-l: List table names
|
|
|
|
'
|
|
}
|
|
[[ "$1" == "-h" ]] && {
|
|
help; exit
|
|
}
|
|
[[ -f "$1" ]] || {
|
|
help; exit
|
|
}
|
|
|
|
sqlfile="$1"
|
|
[[ "$2" == "-l" ]] && {
|
|
sqlite3 "$sqlfile" "SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'"
|
|
exit
|
|
}
|
|
|
|
[[ -z "$2" ]] && {
|
|
table=$( sqlite3 "$sqlfile" "SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite%' LIMIT 1;" )
|
|
} || {
|
|
table="$2"
|
|
}
|
|
[[ "$table" = "${table% *}" ]] && {
|
|
query="SELECT * FROM $table;"
|
|
} || {
|
|
query="$table"
|
|
}
|
|
VIEWER="tabview -w max -"
|
|
which tabview > /dev/null || VIEWER="cat -"
|
|
|
|
sqlite3 -header -separator ' ' -nullvalue NA "$sqlfile" "$query" | $VIEWER
|
|
|