やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

SQLite3ドットコマンド(.read)

 ファイルから読み込んだSQL文やドットコマンドを実行する。

成果物

.help

.read FILE               Read input from FILE

.read

 .readは指定したファイルから読み込んだSQL文やドットコマンドを実行する。

SQL

echo

 SQLファイルを作成する。

echo "create table T(C text);
insert into T values('AAA');" > create.sql

 .readしてテーブルやレコードを確認する。

sqlite3 :memory: \
".read create.sql" \
".tables" \
"select * from T;"

 実行結果は以下。

T
AAA

.dump

 .dumpSQLファイルを作成する。.dumpについては次回

sqlite3 :memory: \
"create table T(C text);" \
"insert into T values('AAA');" \
".dump" > dump.sql

 .readSQLファイルを読み込んで実行する。

sqlite3 :memory: \
".read dump.sql" \
".tables" \
"select * from T;"
T
AAA

SQLite3専用SQL文プラグマ

echo "pragma function_list;" > func_list.sql
sqlite3 :memory: \
".read func_list.sql"

結果

group_concat|1
julianday|1
ntile|1
nullif|1
sqlite_compileoption_get|1
current_timestamp|1
sqlite_compileoption_used|1
sum|1
quote|1
printf|1
likelihood|1
last_value|1
rank|1
round|1
rtrim|1
nth_value|1
random|1
trim|1
time|1
total|1
substr|1
replace|1
upper|1
typeof|1
load_extension|1
avg|1
abs|1
strftime|1
randomblob|1
unicode|1
percent_rank|1
row_number|1
last_insert_rowid|1
sqlite_log|1
unlikely|1
char|1
count|1
date|1
sqlite_record|1
total_changes|1
changes|1
sqlite_version|1
coalesce|1
glob|1
zeroblob|1
hex|1
sqlite_source_id|1
datetime|1
cume_dist|1
instr|1
dense_rank|1
ifnull|1
current_date|1
current_time|1
lag|1
like|1
max|1
min|1
lead|1
lower|1
ltrim|1
first_value|1
length|1
likely|1
edit|0
shell_int32|0
shell_putsnl|0
shell_module_schema|0
writefile|0
shell_escape_crnl|0
readfile|0
json_valid|0
json_type|0
json_set|0
json_extract|0
json_array_length|0
snippet|0
geopoly_overlap|0
fts5_fold|0
shell_add_schema|0
json_insert|0
fts5_expr_tcl|0
fts5|0
fts3_tokenizer|0
fts5_isalnum|0
geopoly_xform|0
icu_load_collation|0
fts5_expr|0
upper|0
json_array|0
zipfile_cds|0
fts5_rowid|0
fts5_source_id|0
offsets|0
fts5_decode_none|0
lsmode|0
json_group_array|0
json_object|0
geopoly_contains_point|0
highlight|0
sha3_query|0
rtreenode|0
sqlar_compress|0
json|0
rtreecheck|0
json_group_object|0
geopoly_regular|0
fts5_decode|0
json_quote|0
matchinfo|0
geopoly_debug|0
sha3|0
geopoly_bbox|0
match|0
lower|0
bm25|0
geopoly_svg|0
json_replace|0
optimize|0
regexp|0
geopoly_ccw|0
zipfile|0
json_patch|0
like|0
sqlar_uncompress|0
geopoly_blob|0
json_remove|0
geopoly_within|0
rtreedepth|0
geopoly_area|0
geopoly_json|0
geopoly_group_bbox|0

 ターミナルで以下コマンド実行。

sqlite3 :memory: "create table T(C text);" "insert into T values('AAA');" ".dump"
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE T(C text);
INSERT INTO T VALUES('AAA');
COMMIT;

ドットコマンド

 SQLファイルを作成する。

echo "create table T(C text);
insert into T values('AAA');
.tables
.headers on
.mode column
select * from T;
" > create_dot.sql

 .readしてテーブルやレコードを確認する。

sqlite3 :memory: ".read create_dot.sql"

 実行結果は以下。

T
C         
----------
AAA       

対話モードから実行

 ターミナルで以下コマンド実行。

sqlite3

 対話モードになる。

sqlite> 

 まだテーブルが1つも存在しないことを確認する。

.tables

 .readコマンド実行。

.read create.sql

 create.sqlファイルによりテーブルとレコードが作成されたことを確認。

.tables
T
select * from T;
AAA

CLIで同じことをやる

 リダイレクトすればいい。

sqlite3 :memory: < create_dot.sql
T
C         
----------
AAA       

1行ずつコマンドを渡す

 ファイルでなくコマンド引数としてSQLやドットコマンドを渡す。

sqlite3 :memory: \
"create table T(C text);" \
"insert into T values('AAA');" \
".tables" \
".headers on" \
".mode column" \
"select * from T;"

 出力結果。

T
C         
----------
AAA       

出力結果をファイルへ

 リダイレクトする。

sqlite3 :memory: \
"create table T(C text);" \
"insert into T values('AAA');" \
".tables" \
".headers on" \
".mode column" \
"select * from T;" > out.txt

 出力結果をみる。

cat out.txt
T
C         
----------
AAA       

環境変数を使う

 たとえばselectするテーブル名を環境変数にする。

TBL_NM="T"
sqlite3 :memory: \
"create table T(C text);" \
"insert into T values('AAA');" \
".tables" \
".headers on" \
".mode column" \
"select * from ${TBL_NM};" > out.txt
cat out.txt
T
C         
----------
AAA       

類似コマンド

  • .load: 拡張ライブラリ*.soファイルを読み込む
  • .open: DBファイルを開く

対コマンド

  • .output: 出力先を指定する(任意ファイルパス or stdout

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux

前回まで