SQLite 3 でミリ秒付きの日時を扱う方法
【概要】SQLite 3においてミリ秒を含む日時データを扱う方法を解説する。テーブル定義、レコード挿入、検索方法、UNIXタイムスタンプ形式での表示方法を具体例とともに示す。
【目次】
前準備
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タイムスタンプ形式での表示
select strftime("%s", created_at) + strftime("%f", created_at) - round( strftime("%f", created_at) ), created_at from R;
SQLite 3の終了手順
.exit