「SQLite3」insert構文に現在の日付時刻を入れるサンプル

環境
Windows 11 Pro 21H2 64bit
SQLite 3.39.2

構文
insert into テーブル名(列名) values (datetime('now’, 'localtime’))
datetime('now’, 'localtime’)を使って現在の日付時刻を取得します。

使用例
1.データベースを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\study\sqlite>sqlite3 test.sqlite3
SQLite version 3.39.2 2022-07-21 15:24:47
Enter ".help" for usage hints.
sqlite>
C:\study\sqlite>sqlite3 test.sqlite3 SQLite version 3.39.2 2022-07-21 15:24:47 Enter ".help" for usage hints. sqlite>
C:\study\sqlite>sqlite3 test.sqlite3
SQLite version 3.39.2 2022-07-21 15:24:47
Enter ".help" for usage hints.
sqlite>

2.テーブルを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sqlite> create table usertbl(uid,uname,regdate);
sqlite> create table usertbl(uid,uname,regdate);
sqlite> create table usertbl(uid,uname,regdate);

3.現在の日付を挿入します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sqlite> insert into usertbl values('2002','user002',datetime('now', 'localtime'));
sqlite> insert into usertbl values('2002','user002',datetime('now', 'localtime'));
sqlite> insert into usertbl values('2002','user002',datetime('now', 'localtime'));

4.挿入結果を確認します
SQL構文
sqlite> select * from usertbl where uid =’2002′;
実行結果
2002|user002|2022-09-13 07:18:28

SQLite

Posted by arkgame