0%

using mysql datetime and datetime(6) with c++ and sql

datetime

no fraction part.

create table

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE IF NOT EXISTS `camera` (\
`id` int(11) NOT NULL AUTO_INCREMENT,\
`camera_id` tinyint(4) UNSIGNED NOT NULL,\
`create_time` datetime NOT NULL,\
PRIMARY KEY(`id`)\
)\
ENGINE = InnoDB\
DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci\
AUTO_INCREMENT = 0\
ROW_FORMAT = DYNAMIC\
;

camera.CreateTime of type time_t

insert

sql

1
INSERT INTO camera(camera_id,create_time) VALUES ( ?,FROM_UNIXTIME(?) );

code

1
pstmt->setUInt64(2, camera.CreateTime); // unix_ts(integer) ===>mysql_ts(string)

select

sql

1
SELECT id,camera_id,UNIX_TIMESTAMP(create_time) as unix_ts FROM camera;

code

1
camera.CreateTime = res->getUInt64("unix_ts");

datetime(6)

with fraction part: millisecond,microsecond

Reference

History

  • 20180118: created.