SQLite 資料一覧

クエリの出力形式を変更する

2008/4/10更新

対応バージョン: 3.5.7

クエリのデフォルトの出力は通常1レコードにつき1行で、ヘッダなし、区切り文字が「|」である。

sqlite> select * from customer;
1|foo|01-234-5678|park
2|bar|99-999-9999|apart
3|baz||

この出力形式を変更するにはまず現在の設定を.showで確認し、必要に応じて各種設定を変更するとよい。

sqlite> .show
     echo: off
  explain: off
  headers: off
     mode: list
nullvalue: ""
   output: stdout
separator: "|"
    width:

以下、それぞれの設定を説明する。

.echo ON|OFF

コマンドechoのON/OFFを切り替える。

sqlite> .echo ON

sqlite> select * from customer;
select * from customer; ← コマンドecho
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||

.explain ON|OFF

EXPLAIN(説明用)に適した設定に複数パラメータをまとめて変更する。

.explain OFF(デフォルト)
sqlite> .show
     echo: off
  explain: off
  headers: off
     mode: list
nullvalue: ""
   output: stdout
separator: "|"
    width: 

sqlite> select * from customer;
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||
.explain ON
sqlite> .explain ON

sqlite> .show
     echo: off
  explain: on ← 変更
  headers: on ← 変更
     mode: explain ← 変更
nullvalue: ""
   output: stdout
separator: "|"
    width: 4 13 4 4 4 13 2 13 ← 変更 

sqlite> select * from customer;
id    name           tel   addr
----  -------------  ----  ----
1     foo            01-234-5789  park
2     bar            99-999-9999  apart
3     baz                      

.headers ON|OFF

ヘッダ出力のON/OFFを切り替える。

sqlite> .headers ON

sqlite> select * from customer;
id|name|tel|address ← ヘッダ
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||

.mode

.modeを使うことでデータの出力形式を変更できる。

以下、指定可能な出力形式を示す。

csv

カンマ区切りのCSVとして出力する。

sqlite> .mode csv customer

sqlite> select * from customer;
1,foo,01-234-5789,park
2,bar,99-999-9999,apart
3,baz,"",""

.nullvalueの設定でNULL値の代替文字列を指定できる。

sqlite> .nullvalue "N/A"

sqlite> select * from customer;
1,foo,01-234-5789,park
2,bar,99-999-9999,apart
3,baz,N/A,N/A
column

カラム毎にスペースで区切って桁揃えをしつつ出力する。

sqlite> .mode column customer

sqlite> select * from customer;
1           foo         01-234-5789  park
2           bar         99-999-9999  apart
3           baz

各カラムの桁は.widthで指定できる。各カラムともデフォルトは0(自動調整)。

sqlite> .width 3 8 12 16 customer

sqlite> select * from customer;
1    foo       01-234-5789   park
2    bar       99-999-9999   apart
3    baz
html

HTMLのtableタグとして出力する。

sqlite> .mode html customer

sqlite> select * from customer;
<TR><TD>1</TD>
<TD>foo</TD>
<TD>01-234-5789</TD>
<TD>park</TD>
</TR>
<TR><TD>2</TD>
<TD>bar</TD>
<TD>99-999-9999</TD>
<TD>apart</TD>
</TR>
<TR><TD>3</TD>
<TD>baz</TD>
<TD></TD>
<TD></TD>
</TR>
insert

SQLのinsert文として出力する。

sqlite> .mode insert customer

sqlite> select * from customer;
INSERT INTO customer VALUES(1,'foo','01-234-5789','park');
INSERT INTO customer VALUES(2,'bar','99-999-9999','apart');
INSERT INTO customer VALUES(3,'baz','','');
line

1カラムにつき1行出力する。

sqlite> .mode line customer

sqlite> select * from customer;
     id = 1
   name = foo
    tel = 01-234-5789
address = park

     id = 2
   name = bar
    tel = 99-999-9999
address = apart

     id = 3
   name = baz
    tel = 
address = 
list

1レコードにつき1行出力するデフォルトの形式である。

sqlite> .mode list customer

sqlite> select * from customer;
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||

.separatorで区切り文字を変更できる。

個々のテーブルに対する変更はできず、全てのテーブルが対象となる。

sqlite> .separator ":"

sqlite> select * from customer;
1:foo:01-234-5789:park
2:bar:99-999-9999:apart
3:baz::
tabs

カラム毎にタブで区切って出力する。

sqlite> .mode tab customer

sqlite> select * from customer;
1       foo     01-234-5789     park
2       bar     99-999-9999     apart
3       baz
tcl

Tclのリスト形式で出力する。

sqlite> .mode tcl customer

sqlite> select * from customer;
"1"     "foo"   "01-234-5789"   "park"
"2"     "bar"   "99-999-9999"   "apart"
"3"     "baz"   ""      ""

.output stdout|<ファイル名>

通常のstdoutへの出力をファイルに切り替える。

qlite> select * from customer;
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||

sqlite> .output data.out

sqlite> select * from customer;
(何も出力されない)

sqlite> .q

% cat data.out
1|foo|01-234-5789|park
2|bar|99-999-9999|apart
3|baz||