Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅将历元字符串保存到mysql datetime年/月/日,但不保存时分秒 < >我有一些C++代码,它保存了一个历元字符串(从时间> 对象)转换为mysql数据库时间列(数据类型日期时间< /代码>)。 C++中的历元时间字符串是“代码>1559333386”/CODE,但是,当保存到MySQL时,只显示日期、小时、分钟和秒都显示为零: 2019-05-3100:00: < /P>_C++_Mysql_Datetime - Fatal编程技术网

仅将历元字符串保存到mysql datetime年/月/日,但不保存时分秒 < >我有一些C++代码,它保存了一个历元字符串(从时间> 对象)转换为mysql数据库时间列(数据类型日期时间< /代码>)。 C++中的历元时间字符串是“代码>1559333386”/CODE,但是,当保存到MySQL时,只显示日期、小时、分钟和秒都显示为零: 2019-05-3100:00: < /P>

仅将历元字符串保存到mysql datetime年/月/日,但不保存时分秒 < >我有一些C++代码,它保存了一个历元字符串(从时间> 对象)转换为mysql数据库时间列(数据类型日期时间< /代码>)。 C++中的历元时间字符串是“代码>1559333386”/CODE,但是,当保存到MySQL时,只显示日期、小时、分钟和秒都显示为零: 2019-05-3100:00: < /P>,c++,mysql,datetime,C++,Mysql,Datetime,问题是如何以year-month-day-hour:minute:second格式将包含完整信息的历元时间戳保存到MySQL数据库中 谢谢 代码如下: std::time_t t = std::stof(time); std::stringstream sstime; sstime << std::put_time(std::gmtime(&t), "%F") << '\n'; Poco::Data::Statement sql_insert(*session);

问题是如何以
year-month-day-hour:minute:second
格式将包含完整信息的历元时间戳保存到MySQL数据库中

谢谢

代码如下:

std::time_t t = std::stof(time);
std::stringstream sstime;
sstime << std::put_time(std::gmtime(&t), "%F") << '\n';
Poco::Data::Statement sql_insert(*session);
sql_insert << "INSERT into history (uid,  time) \
               VALUES (?,?)",
               Poco::Data::Keywords::bind(uid),

               Poco::Data::Keywords::bind(sstime.str());
std::time\u t=std::stof(time);
std::stringstream sstime;

sstime以下是两种可能的解决方案:

一种是对历元时间戳进行一些预处理,将其格式更改为
YYYY-MM-DD HH24:MI:SS
,然后将其绑定到SQL语句

另一个在我看来有点奇怪和危险,我在MySQL网站上的评论部分找到了它,下面是如何:

SET@@timestamp:=1559333386;
插入历史(时间)值(现在());
SET@@timestamp:=默认值;
有关更多信息,请参阅的MySQL文档


以上只是一些建议,我没有在没有理想环境的情况下进行任何测试和验证,请在任何地方实施之前进行彻底测试

下面是代码:
std::time\u t t=std::stof(time);std::stringstream sstime;同时,请删除注释中的代码。如果有必要,你可以改为编辑你的帖子。谢谢你的评论,我会尝试这两种方法。