SQLite 3 でミリ秒付きの日時を扱う例
前準備
SQLite 3 について: 別ページ »にまとめ
ミリ秒を格納するテーブルのテーブル定義,レコードの挿入
SQLite 3の起動
ここでは, データベース名を指定せず,インメモリ・データベースを用いている.
sqlite3
現在の日時
select datetime('now', 'localtime');
日時を格納したテーブルの作成例
create table R ( id integer primary key not null, created_at datetime); insert into R values( 1, '2020-06-25 19:30:30.001' ); insert into R values( 2, '2020-06-25 19:30:30.002' ); insert into R values( 3, '2020-06-25 19:30:30.003' ); insert into R values( 4, '2020-06-25 19:30:31.001' );
問い合わせの例
select * from R; select * from R where created_at < '2020-06-25 19:30:31.001'; select * from R where '2020-06-25 19:30:30.002' < created_at AND created_at < '2020-06-25 19:30:31.001';
UNIX time で表示
select strftime("%s", created_at) + strftime("%f", created_at) - round( strftime("%f", created_at) ), created_at from R;
SQLite 3の終了
.exit