tech-memo

How to use sqlite3

使い方

sqlite3 
.quit

または

;

で終了する

headers

デフォルトでselectするとカラム名が表示されない.

.headers on

で表示させる.

メタsql

テーブル一覧

.tables

カラム一覧

PRAGMA table_info([テーブル名]);

CSVでインポート

.mode csv
.import --skip 1 /path/to/user_1001.csv user

skipはヘッダなどをスキップ

sql

現在時刻、タイムスタンプ

current_timestamp

日付演算・表示

datetime(current_timestamp ,'-3 days')
date(current_timestamp ,'-3 days')
strftime('%Y-%m-%d', timestamp)http://localhost:3000/summaria

テーブルバックアップ

create table table1_bak as select * from table1;

truncate

truncate tableはないので、delete from

副問合せによる更新

update manual_contents_list set description = manual_contents_list_bk.description from manual_contents_list join manual_contents_list_bk
on manual_contents_list.pagepath = manual_contents_list_bk.pagepath;
UPDATE manual_contents_list
SET description = (
    SELECT manual_contents_list_bk.description
    FROM manual_contents_list_bk
    WHERE manual_contents_list.pagepath = manual_contents_list_bk.pagepath
)
WHERE EXISTS (
    SELECT 1
    FROM manual_contents_list_bk
    WHERE manual_contents_list.pagepath = manual_contents_list_bk.pagepath
);

テーブルのリネーム

ALTER TABLE old_table_name RENAME TO new_table_name;

sql文の吐き出し

sqlite3 [データベースファイル]
> .output [出力したいsqlファイル]
> .dump [テーブル名(未指定なら全テーブル)]
> .quit 

sql文の実行

sqlite3 [データベースファイル] < [sqlファイル]