欢迎来到科站长!

SQLite

当前位置: 主页 > 数据库 > SQLite

sqlite3自动插入创建时间和更新时间的功能实现

时间:2024-09-22 15:18:52|栏目:SQLite|点击:

最近在记录一些简单的结构化日志信息时,用到了sqlite3数据库(保存的信息比较简单,用MysqlSQL ServerPostgres这些数据库有点小题大做)。

以前开发系统时,用MysqlPostgres比较多,sqlite3接触不多,这次使用,希望sqlite3也能提供几个基本的功能,比如:

  • 主键ID自增
  • 插入数据时,自动更新创建时间created_at
  • 更新数据时,自动更新更新时间updated_at

调查这几个功能的过程记录如下。

1. 准备

首先创建一个数据库,sqlite3数据库其实就是一个文件。

1
2
3
4
$  sqlite3.exe test.db
SQLite version 3.41.2 2023-03-22 11:56:21
Enter ".help" for usage hints.
sqlite>

这里不需要管 test.db 文件存不存在,如果不存在,会自动创建的。

创建一张表 position_info,这是我用来记录账户净值和利润的表,其中字段的作用不用管,只需要关注 idcreated_atupdated_at三个字段即可。

1
2
3
4
5
6
7
sqlite> CREATE TABLE IF NOT EXISTS position_info (
(x1...>     id INTEGER NOT NULL PRIMARY KEY,
(x1...>     equity REAL NOT NULL,
(x1...>     profit_loss REAL NOT NULL,
(x1...>     created_at TEXT NOT NULL,
(x1...>     updated_at TEXT NOT NULL
(x1...> );

创建之后,通过sqlite3的命令查看position_info表是否创建。

1
2
sqlite> .tables
position_info

sqlite3的自带命令都是以点号.)开头的。

表按照默认的方式创建之后, 发现插入一条数据很麻烦,需要指定position_info表中所有5个字段才能插入成功。

1
2
3
4
5
6
7
8
9
10
sqlite> insert INTO position_info(id, equity,
(x1...>  profit_loss, created_at, updated_at)
   ...>  VALUES(1, 10, 2,
(x1...>   "2024-06-09 10:10:10", "2024-06-09 10:10:10");
 
sqlite> .headers on
 
sqlite> select * FROM position_info;
id|equity|profit_loss|created_at|updated_at
1|10.0|2.0|2024-06-09 10:10:10|2024-06-09 10:10:10

其实,我希望实现的是插入和更新时,只关注equityprofit_loss两个字段,其他3个字段由数据库自动管理。

类似:insert INTO position_info(equity, profit_loss) VALUES(10, 2);

下面开始改造。

2. 主键ID自增

首先,让主键ID能够自动增长。

1
2
3
4
5
6
7
8
9
10
sqlite> drop table position_info;
sqlite> CREATE TABLE IF NOT EXISTS position_info (
(x1...>     id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
(x1...>         equity REAL NOT NULL,
(x1...>         profit_loss REAL NOT NULL,
(x1...>     created_at TEXT NOT NULL,
(x1...>     updated_at TEXT NOT NULL
(x1...> );
sqlite> select * from position_info;
sqlite>

先删除创建的 position_info,然后重新创建position_info表,创建时指定id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT

创建完成后,插入两条数据,插入时不指定ID字段,发现数据库会帮我们自动插入ID。

sqlite> insert INTO position_info(equity,
(x1...>  profit_loss, created_at, updated_at)
   ...>  VALUES(10, 2,
(x1...>   "2024-06-09 10:10:10", "2024-06-09 10:10:10");

sqlite> insert INTO position_info(equity,
(x1...>  profit_loss, created_at, updated_at)
   ...>  VALUES(100, 20,
(x1...>   "2024-06-09 11:11:11", "2024-06-09 11:11:11");

sqlite> select * from position_info;
id|equity|profit_loss|created_at|updated_at
1|10.0|2.0|2024-06-09 10:10:10|2024-06-09 10:10:10
2|100.0|20.0|2024-06-09 11:11:11|2024-06-09 11:11:11

3. 创建时间(created_at)

接下来,设置创建时间created_at)和更新时间updated_at)自动插入:DEFAULT (DATETIME('now', 'localtime'))

sqlite> drop table position_info;
sqlite> CREATE TABLE IF NOT EXISTS position_info (
(x1...>     id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
(x1...>         equity REAL NOT NULL,
(x1...>         profit_loss REAL NOT NULL,
(x1...>     created_at TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')),
(x1...>     updated_at TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime'))
(x1...> );

然后插入两条测试数据:

sqlite> insert INTO position_info(equity, profit_loss)
   ...>  VALUES(10, 2);
sqlite>
sqlite> insert INTO position_info(equity, profit_loss)
   ...>  VALUES(100, 20);

sqlite> select * from position_info;
id|equity|profit_loss|created_at|updated_at
1|10.0|2.0|2024-06-09 16:40:52|2024-06-09 16:40:52
2|100.0|20.0|2024-06-09 16:40:53|2024-06-09 16:40:53


上一篇:暂无

栏    目:SQLite

下一篇:SQLite 转换字符串为日期的示例代码

本文标题:sqlite3自动插入创建时间和更新时间的功能实现

本文地址:https://www.fushidao.cc/shujuku/804.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 科站长 版权所有冀ICP备14023439号