ラベル column の投稿を表示しています。 すべての投稿を表示
ラベル column の投稿を表示しています。 すべての投稿を表示

2026年3月21日土曜日

SQLite 3 .mode で幅広文字の表示が改善された

概要

コマンドラインツールには select 文の結果表示モードを変更する .mode コマンドがある。

今までだと、このコマンドで指定できるモードのうち boxcolumn では 幅広の文字を表示しようとすると体裁が崩れてしまっていた。

ver 3.51.0 でこれが改善され、日本語のひらがなや漢字でも体裁が崩れることなく表示されるようになった。

構文

改善された表示モードは以下の通り。


.mode box
.mode column
.mode markdown
.mode qbox
.mode table
                    

実行例

まずは古いバージョンでの見え方。

環境
  • Windows 11
  • SQLite3 Command-Line Shell ver 3.49.0

sqlite> .version
SQLite 3.49.0 2025-02-06 11:55:18 4a7dd425dc2a0e5082a9049c9b4a9d4f199a71583d014c24b4cfe276c5a77cde
zlib version 1.3
msvc-1939 (64-bit)

sqlite> create table user (id, name);
sqlite> insert into user values ('0001', 'ほげほげ');
sqlite> select * from user;
0001|ほげほげ

sqlite> .mode box
sqlite> select * from user;
┌──────┬──────┐
│  id  │ name │
├──────┼──────┤
│ 0001 │ ほげ │
└──────┴──────┘

sqlite> .mode column
sqlite> select * from user;
id    name
----  ----
0001  ほげ

sqlite> .mode markdown
sqlite> select * from user;
|  id  | name |
|------|------|
| 0001 | ほげ |

sqlite> .mode qbox
sqlite> select * from user;
┌────────┬────────┐
│   id   │  name  │
├────────┼────────┤
│ '0001' │ 'ほげほげ'  │
└────────┴────────┘

sqlite> .mode table
sqlite> select * from user;
+------+------+
|  id  | name |
+------+------+
| 0001 | ほげ |
+------+------+
                        

上記の例では、「name」カラムに「ほげほげ」というデータが入っているが、 表示モードを切り替えることで体裁が崩れ、データが欠けてしまっていることがわかる。

これが ver 3.51.0 以降は改善され、表示されるようになる。

環境
  • Windows 11 64bit
  • SQLite Command-Line Shell ver 3.51.3

sqlite> .version
SQLite 3.51.3 2026-03-13 10:38:09 737ae4a34738ffa0c3ff7f9bb18df914dd1cad163f28fd6b6e114a344fe6d618
zlib version 1.3
msvc-1939 (64-bit)

sqlite> create table user (id, name);
sqlite> insert into user values ('0001', 'ほげほげ');
sqlite> select * from user;
0001|ほげほげ

sqlite> .mode box
sqlite> select * from user;
┌──────┬──────────┐
│  id  │   name   │
├──────┼──────────┤
│ 0001 │ ほげほげ │
└──────┴──────────┘

sqlite> .mode column
sqlite> select * from user;
id    name
----  --------
0001  ほげほげ

sqlite> .mode qbox
sqlite> select * from user;
┌────────┬────────────┐
│   id   │    name    │
├────────┼────────────┤
│ '0001' │ 'ほげほげ' │
└────────┴────────────┘

sqlite> .mode markdown
sqlite> select * from user;
|  id  |   name   |
|------|----------|
| 0001 | ほげほげ |


sqlite> .mode table
sqlite> select * from user;
+------+----------+
|  id  |   name   |
+------+----------+
| 0001 | ほげほげ |
+------+----------+
                        

参考URL

2020年11月14日土曜日

SQLite 3 コマンドラインツールでselectの結果を見やすくする

概要

SQLite3 のコマンドラインツールは、 デフォルトだとselect結果の表示の列が揃っておらず 区切り文字も「|」になっていて見づらい。 列をそろえて表示するには「.mode」コマンドで出力モードを変更する。

「.mode」で変更できる出力モードは複数あるが、 コマンドラインツールで見やすさを求めるなら「box」「column」「markdown」「table」がいい。 個人的には「box」が一番見やすい。

環境
  • Windows 10 64bit
  • SQLite3 (3.33.0) Command-Line Shell
サンプルテーブル(product)
id
primary key
autoincrement
name
not null
quantity
default 0
remark
1 tomato 100
2 potato 120
3 pumpkin 50 (Comment...)

boxの表示例

headers:off の状態でもカラム名は自動的に表示される


sqlite> .mode box
sqlite> select * from product;
┌────┬─────────┬──────────┬──────────────┐
│ id │  name   │ quantity │    remark    │
├────┼─────────┼──────────┼──────────────┤
│ 1  │ tomato  │ 100      │              │
│ 2  │ potato  │ 120      │              │
│ 3  │ pumpkin │ 50       │ (Comment...) │
└────┴─────────┴──────────┴──────────────┘
                

columnsの表示例

強制的にheaders:onに変更される。 「.headers off」とするとカラム名を非表示にできる。


sqlite> .mode column
sqlite> select * from product;
id  name     quantity  remark
--  -------  --------  ------------
1   tomato   100
2   potato   120
3   pumpkin  50        (Comment...)

sqlite> .headers off
sqlite> select * from product;
1   tomato   100
2   potato   120
3   pumpkin  50        (Comment...)
                

markdownの表示例

markdown記法に従った形式で表示される。 headers:off の状態でもカラム名は自動的に表示される。


sqlite> .mode markdown
sqlite> select * from product;
| id |  name   | quantity |    remark    |
|----|---------|----------|--------------|
| 1  | tomato  | 100      |              |
| 2  | potato  | 120      |              |
| 3  | pumpkin | 50       | (Comment...) |
                

tableの表示例

headers:off の状態でもカラム名は自動的に表示される


sqlite> .mode table
sqlite> select * from product;
+----+---------+----------+--------------+
| id |  name   | quantity |    remark    |
+----+---------+----------+--------------+
| 1  | tomato  | 100      |              |
| 2  | potato  | 120      |              |
| 3  | pumpkin | 50       | (Comment...) |
+----+---------+----------+--------------+
                

参考URL