C++获取系统时间如何插入到MySQL里面的datetime型属性中

用c++获取系统的时间后,发现时间的格式是int型,并且我们需要的格式是类似2015-07-24 15:55:03这种类型的格式,为此将这些int型的年月日时分秒转换为string,而MySQL中datetime型的格式为'2015-07-24 15:55:03',那么问题来了:怎么将"2015-07-24 15:55:03"转换成'2015-07-24 15:55:03',从而sql语句能够插入当前时间。

相关代码如下:

[cpp]  view plain  copy
  1. //get the current time  
  2. CTime t = CTime::GetCurrentTime();//#include <atltime.h>  
  3. string mytime = inttostring(t.GetYear()) + "-" + inttostring(t.GetMonth()) + "-" + inttostring(t.GetDay())  
  4.     + " " + inttostring(t.GetHour()) + ":" + inttostring(t.GetMinute()) + ":" + inttostring(t.GetSecond());  
  5. cout << mytime << endl;  
  6.   
  7. char sdate[30];  
  8. strcpy_s(sdate, mytime.c_str());  
  9.   
  10. char mysql[] = "insert into tb_report(source_id,created_time) values(1,'%s')";  
  11. char mysqlBuf[1024];  
  12. sprintf_s(mysqlBuf, mysql, sdate);  
  13. res = mysql_query(&myCont, mysqlBuf);  

int转string:

[cpp]  view plain  copy
  1. //int to string  
  2. string inttostring(int in)  
  3. {  
  4.     stringstream ss;  
  5.     string str;  
  6.     ss << in;  
  7.     ss >> str;  
  8.     return str;  
  9. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值