SQLiteクライアント litecli でシンタックスハイライトと補完
litecli
litecliはpgcliやmycliと同じく補完とシンタックスハイライトができるSQLiteクライアント。

インストール
pipでインストールできる。
$ pip install litecli
macOSならbrewでのインストールも可能。
$ brew tap dbcli/tap $ brew install litecli
ヘルプメッセージ。
$ litecli --version
Version: 1.0.0
$ litecli --help
Usage: litecli [OPTIONS] [DATABASE]
A SQLite terminal client with auto-completion and syntax highlighting.
Examples:
- litecli lite_database
Options:
-V, --version Output litecli's version.
-D, --database TEXT Database to use.
-R, --prompt TEXT Prompt format (Default: "\d> ").
-l, --logfile FILENAME Log every query and its results to a file.
--liteclirc FILE Location of liteclirc file.
--auto-vertical-output Automatically switch to vertical output mode if the
result is wider than the terminal width.
-t, --table Display batch output in table format.
--csv Display batch output in CSV format.
--warn / --no-warn Warn before running a destructive query.
-e, --execute TEXT Execute command and quit.
--help Show this message and exit.
またデータベース接続時に\?で以下のようなヘルプを表示できる。
db.sqlite3> \? +------------+----------------------------+------------------------------------------------------------+ | Command | Shortcut | Description | +------------+----------------------------+------------------------------------------------------------+ | .databases | .databases | List databases. | | .exit | \q | Exit. | | .mode | \T | Change the table format used to output results. | | .once | \o [-o] filename | Append next result to an output file (overwrite using -o). | | .open | .open | Change to a new database. | | .schema | .schema[+] [table] | The complete schema for the database or a single table | | .status | \s | Show current settings. | | .tables | \dt[+] [table] | List or describe tables. | | \G | \G | Display current query results vertically. | | \e | \e | Edit command with editor (uses $EDITOR). | | \f | \f [name [args..]] | List or execute favorite queries. | | \fd | \fd [name] | Delete a favorite query. | | \fs | \fs name query | Save a favorite query. | | help | \? | Show this help. | | nopager | \n | Disable pager, print to stdout. | | notee | notee | Stop writing results to an output file. | | pager | \P [command] | Set PAGER. Print the query results via PAGER. | | prompt | \R | Change prompt format. | | quit | \q | Quit. | | rehash | \# | Refresh auto-completions. | | source | \. filename | Execute commands from file. | | system | system [command] | Execute a system shell commmand. | | tee | tee [-o] filename | Append all results to an output file (overwrite using -o). | | watch | watch [seconds] [-c] query | Executes the query every [seconds] seconds (by default 5). | +------------+----------------------------+------------------------------------------------------------+
使い方
sqlite3の代わりにlitecliコマンドを使えばよい。
$ litecli <db_name>
入力すると図のようにコンテクストに応じてテーブル名、カラム名などが補完される。



またコマンド履歴からの補完もされる。

設定ファイル
設定ファイルは~/.config/litecli/configに初回起動時に作成されるとのこと。
設定ファイルの詳細はこのページを参照。
カラーテーマ
選択可能なカラーテーマはこのページに書いてある。
設定ファイルの下記行を変更すると反映される。
# Syntax coloring style. Possible values (many support the "-dark" suffix): # manni, igor, xcode, vim, autumn, vs, rrt, native, perldoc, borland, tango, emacs, # friendly, monokai, paraiso, colorful, murphy, bw, pastie, paraiso, trac, default, # fruity. # Screenshots at http://mycli.net/syntax syntax_style = default
下図はmonokaiに変更した例。

クエリの保存
よく使うクエリに名前を付けて保存することができる。
\fで保存したクエリのリストを確認できる。
db.sqlite3> \f
No favorite queries found.
Favorite Queries are a way to save frequently used queries
with a short name.
Examples:
# Save a new favorite query.
> \fs simple select * from abc where a is not Null;
# List all favorite queries.
> \f
╒════════╤═══════════════════════════════════════╕
│ Name │ Query │
╞════════╪═══════════════════════════════════════╡
│ simple │ SELECT * FROM abc where a is not NULL │
╘════════╧═══════════════════════════════════════╛
# Run a favorite query.
> \f simple
╒════════╤════════╕
│ a │ b │
╞════════╪════════╡
│ 日本語 │ 日本語 │
╘════════╧════════╛
# Delete a favorite query.
> \fd simple
simple: Deleted
Time: 0.000s
No favorite query:
Time: 0.000s
\fsの後に名前、SQL文を入力して実行することで保存でき、\fと名前で保存したSQL文を実行できる。
db.sqlite3> \fs sample select * from test; Saved. Time: 0.001s db.sqlite3> \f +--------+--------------------+ | Name | Query | +--------+--------------------+ | sample | select * from test | +--------+--------------------+ Time: 0.038s No favorite query: Time: 0.000s db.sqlite3> \f sample > select * from test +------+------+ | col1 | col2 | +------+------+ | 1 | a | | 2 | b | | 3 | c | +------+------+ Time: 0.041s
引数ありの場合は$1, $2, $3のようなパラメータを使用する。
db.sqlite3> \fs sample2 select * from test where col1 = $1; Saved. Time: 0.001s db.sqlite3> \f +---------+------------------------------------+ | Name | Query | +---------+------------------------------------+ | sample | select * from test | | sample2 | select * from test where col1 = $1 | +---------+------------------------------------+ Time: 0.040s No favorite query: Time: 0.000s db.sqlite3> \f sample2 3 > select * from test where col1 = 3 +------+------+ | col1 | col2 | +------+------+ | 3 | c | +------+------+ Time: 0.041s
なお、保存したクエリは設定ファイルの末尾に追記される。
出力フォーマット
指定可能な出力フォーマットは\Tで確認できる。
db.sqlite3> \T
Table format not recognized. Allowed formats:
vertical
csv
tsv
mediawiki
html
latex
latex_booktabs
textile
moinmoin
jira
plain
simple
grid
fancy_grid
pipe
orgtbl
psql
rst
ascii
double
github
\T <format_name>で一時的に変更できる(常時変更は設定ファイル)。
db.sqlite3> \T ascii Changed table format to ascii Time: 0.000s db.sqlite3> select * from test; +------+------+ | col1 | col2 | +------+------+ | 1 | a | | 2 | b | | 3 | c | +------+------+ 3 rows in set Time: 0.039s db.sqlite3> \T csv Changed table format to csv Time: 0.000s db.sqlite3> select * from test; "col1","col2" "1","a" "2","b" "3","c" 3 rows in set Time: 0.001s db.sqlite3> \T fancy_grid Changed table format to fancy_grid Time: 0.000s db.sqlite3> select * from test; ╒════════╤════════╕ │ col1 │ col2 │ ╞════════╪════════╡ │ 1 │ a │ ├────────┼────────┤ │ 2 │ b │ ├────────┼────────┤ │ 3 │ c │ ╘════════╧════════╛ 3 rows in set Time: 0.039s