
因为要保存爬虫抓取到的时间戳数据,所以不能用 now(),想直接保存整型,但是报错,我记得好像可以直接保存整型的
CREATE TABLE `NewTable` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ts` timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 insert into NewTable(ts) values (1581735917) SQL Error [1292] [22007]: (conn:26) Incorrect datetime value: '1581735917' for column `NewTable`.`ts` at row 1 数据库是 mariadb
搞清楚了。插入的时候需要用from_unixtime转换
insert into NewTable(ts) values (FROM_UNIXTIME(1581735917)) 如果需要保存毫秒数,timestamp字段要指定长度3,比如 timestamp(3),插入时可存浮点数
insert into NewTable(ts) values (FROM_UNIXTIME(1581735917.309)) 1 chenset 2020-02-15 12:39:26 +08:00 1581735917 你写的是 int 类型, 跟数据库的 timestamp 不是同一种类型 |
2 chenset 2020-02-15 12:42:21 +08:00 insert into NewTable(ts) values ('2020-02-15 04:41:22'); 应该这样才行. 未测试~ |
3 chenset 2020-02-15 12:43:57 +08:00 保存 int 型时间戳一般都是这样吧: `ts` int NOT NULL DEFAULT 0 |
4 jugelizi Incorrect datetime value: '117' for 很清楚啊 datatime 类型 你存时间戳了 |
5 turan12 2020-02-15 13:32:57 +08:00 此时间戳非彼时间戳 |
6 mxalbert1996 2020-02-15 14:57:36 +08:00 via Android @chenset datetime 和 timestamp 也不是同一种类型 |
7 leonard916 2020-02-15 15:18:32 +08:00 我得 datetime 比舒服,另外插入要用函 不能直接插一字或者字符串去 |
8 jason19659 2020-02-15 16:21:13 +08:00 你这个 1581735917 至少也要乘以 1000 吧 |