基本操作

ファイルを開く

sqlite3 [file.sqlite3]

終了

.exit / .quit

ヘルプ

.help

テーブル操作

テーブルを作成

create table [テーブル名](フィールド1, フィールド2...);

カラム名は省略可能。例:

create table users(name, email);

テーブル一覧を表示

.tables

テーブルを削除

drop table users;

テーブル名の変更

alter table [テーブル名] rename to [新テーブル名];

テーブルの構造を表示

.schema [テーブル名]

テーブル名を指定しない場合は、全テーブルを表示。

カラム操作

カラムを末尾に追加

alter table [テーブル名] add column [カラム名];

カラムのオプション

[カラム名] integer primary key autoincrement 自動連番
[カラム名] text not null 空欄エラー
[カラム名] text unique ユニークな文字列以外エラー
[カラム名] integer default 20 デフォルト値
[カラム名] check(カラム名>0) 0より小さいとエラー(例)

テーブル作成時にも使用可能。

挿入場所を指定:

[カラム名] first 最初
[カラム名] after [カラム名2] カラム名2の後に追加

データ操作

データを追加

insert into [テーブル名](カラム1, カラム2...) values(値1, 値2...);
insert into users values('yamada', 25);
insert into users(age) values(25);

シングルクォーテーションを書く場合は、2回続けて('It''s a pen.')書く。

nullはシングルクォーテーションで囲わない。

データを変更

update [テーブル名] set [カラム名] = [値] where [条件];
update users set name = 'tanaka' where name = 'yamada';
update users set name = 'tanaka', score = 500 where name = 'yamada';

データを削除

delete from [テーブル名] where [条件];
delete from users where score <= 100;

ROWID

データ番号

取得:

select ROWID, * from users;

削除:

delete from users where ROWID = 4;

データ取得(select)

データを取得

select [カラム名] from [テーブル名];
select * from users;
select name from users;

並び替え昇順(A→ Z) / 降順(Z→ A)

order by [カラム名] / order by [カラム名] desc
select * order by score;
select * order by score desc;

表示件数を指定

limit [数]

select * order by score desc limit 5;

条件を指定

where

select * where score >= 200 order by score;
select * where score is null;

nullを取得するには、is null (isnull)is not null (not null, notnull)を使う。

演算子:+, -, *, /, %=, ==, !=, <>, <, <=, >, >=and, or, not

重複行を削除

distinct

select distinct team from users;

countやsumをグループごとに行う

group by [カラム名]

select team, sum(score) from users group by team;

表示するカラム名を変更

as [別名]

select id, name as username from users;

複数テーブルにまたがる操作

select id, name, team, sum(score) from users, games where users.id = games.user_id group by users.id;

組み込み関数

データの個数を表示

count(*)

select count(*) from users;

最大値 / 最小値を表示

max(カラム名) / min(カラム名)

select max(*) from users;
select max(*) from users;

乱数を生成

random()

select * from users order by random() limit 1;   -- 1つランダムに選ぶ

文字列の長さを表示

length(カラム名)

select name, length(name) from users;

合計を計算

sum(カラム名)

select sum(score) from users;

平均を計算

avg(カラム名)

select avg(score) from users;

データ型を表示

typeof(カラム名)

select name, typeof(name) from users;

日付・時刻

日付 / 時刻 / 日付と時刻

current_date / current_time / current_timestamp

insert into users(name, created) values('yamada', current_timestamp);

日付 / 時刻を指定フォーマットに変換

strfttime()

select strfttime('%Y年%m月%d日', current_timestamp);
%Y
%m 月(01-)
%d 日(00-)
%W 年の初めからの週(00-53)
%j 年の初めからの経過日数(001-366)
%w 曜日(0-6, 日-土)
%H 時(00-24)
%M 分(00-59)
%S 秒(00-59)
%f 秒+ミリ秒(SS.SSS)
%s 1970/1/1からの経過秒数
%% %

画面表示の変更

ヘッダー(カラム名)を表示 / 非表示

.header [on|off]

現在の設定内容を表示

.show

モード(selectの出力形式)を変更

.mode [モード]
mode 書式 メモ
csv カンマ区切り 文字列は""で囲われる
column 指定幅で表示 幅は.widthで変更可能
html HTMLのTABLE <table>は出力されない
insert insert文
line カラムごとに行を分けて中央揃え カラム名と値の間は=
list 区切り文字で区切る 区切り文字は.separatorで変更可能
tabs タブ区切り タブは8文字
tcl |で区切る すべて文字列扱い(""で囲われる)

columnの幅を指定

.width 10
.width 10 5 30

区切り文字を指定

.separator ,

ファイル操作

外部ファイルから読み込み

.import [ファイル名] [テーブル名]

.import users.txt users

区切り文字を.separatorで変更しておく必要がある。

SQL文で書き出し

.dump [テーブル名]

.dump users

出力先ファイルを.outputで指定しておく必要がある。

SQL文を読み込み

.read [ファイル名]

.read users.txt

用語

テーブル

データベースの中のカテゴリ(Excelのシート)

フィールド / カラム

レコード