LoginSignup
3
5

More than 5 years have passed since last update.

sqlite3コマンドを使ったデータ出力方法まとめ

Last updated at Posted at 2016-08-12

SQLite3のデータ出力で使うコマンドをまとめました。

使用環境

  • OSX 10.10.5
  • sqlite3 version 3.8.5

コマンド


使用可能コマンド一覧を表示

コマンドの一覧が表示されます。

sqlite> .help

テーブルのデータ表示

テーブルのデータ表示コマンドです。指定されたフィールドのデータを表示します。フィールド名に*を入れるとテーブル全データを表示します。

sqlite> select フィールド名 from テーブル名;

sqlite> select * from tbl;
1     田中      30       北海道

sqlite> select name from tbl;
田中

ヘッダ表示、非表示

ヘッダにフィールド名を表示するコマンドです。

sqlite> .header on
sqlite> select * from tbl;
id          name        age         place
----------  ----------  ----------  ----------
1           田中         30          北海道

非表示にする時は以下のコマンドです。

sqlite> .header off
sqlite> select * from tbl;
1           田中         30          北海道

データの区切り文字を設定

区切り文字を設定できます。

sqlite> .separator 区切り文字

sqlite> .separator /
sqlite> select * from tbl;
1/田中/30/北海道

データの出力モードを変更

データの出力モードを設定するコマンドです。

sqlite> .mode 出力モード

sqlite> .mode csv
sqlite> select * from tbl;
1,"田中",30,"北海道"

sqlite> .mode column
sqlite> select * from tbl;
1           田中      30          北海道

sqlite> .mode line
sqlite> select * from tbl;
   id = 1
 name = 田中
  age = 30
place = 北海道

sqlite> .mode html
sqlite> select * from tbl;
<TR><TD>1</TD>
<TD>田中</TD>
<TD>30</TD>
<TD>北海道</TD>
</TR>

出力モードは以下のモードでもできます。

出力モード 形式
insert insertコマンドで出力
list リストで出力
tabs タブ区切りで出力
tcl tcl要素で出力

出力幅を設定

出力データの幅を各フィールドごとに設定できます。

sqlite> .width 幅

sqlite> .width 2 6 2 6
sqlite> select * from tbl;
1   田中  30  北海道

sqlite> .width 2 10 5 10
sqlite> select * from tbl;
1   田中      30     北海道

ファイルに出力

ファイルに出力する時は以下のコマンドです。

sqlite> .output 出力ファイル名

sqlite> .output sample.txt

参考文献

西沢直木 「SQLite入門 すぐに使える軽快・軽量データベースエンジン 第2版」 株式会社 翔泳社

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5