C++获取系统时间如何插入到MySQL里面的datetime型属性中
2015-07-24 16:13
615 查看
用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语句能够插入当前时间。
相关代码如下:
//get the current time
CTime t = CTime::GetCurrentTime();//#include <atltime.h>
string mytime = inttostring(t.GetYear()) + "-" + inttostring(t.GetMonth()) + "-" + inttostring(t.GetDay())
+ " " + inttostring(t.GetHour()) + ":" + inttostring(t.GetMinute()) + ":" + inttostring(t.GetSecond());
cout << mytime << endl;
char sdate[30];
strcpy_s(sdate, mytime.c_str());
char mysql[] = "insert into tb_report(source_id,created_time) values(1,'%s')";
char mysqlBuf[1024];
sprintf_s(mysqlBuf, mysql, sdate);
res = mysql_query(&myCont, mysqlBuf);
int转string:
//int to string
string inttostring(int in)
{
stringstream ss;
string str;
ss << in;
ss >> str;
return str;
}
参考:
点击打开链接
15:55:03',从而sql语句能够插入当前时间。
相关代码如下:
//get the current time
CTime t = CTime::GetCurrentTime();//#include <atltime.h>
string mytime = inttostring(t.GetYear()) + "-" + inttostring(t.GetMonth()) + "-" + inttostring(t.GetDay())
+ " " + inttostring(t.GetHour()) + ":" + inttostring(t.GetMinute()) + ":" + inttostring(t.GetSecond());
cout << mytime << endl;
char sdate[30];
strcpy_s(sdate, mytime.c_str());
char mysql[] = "insert into tb_report(source_id,created_time) values(1,'%s')";
char mysqlBuf[1024];
sprintf_s(mysqlBuf, mysql, sdate);
res = mysql_query(&myCont, mysqlBuf);
int转string:
//int to string
string inttostring(int in)
{
stringstream ss;
string str;
ss << in;
ss >> str;
return str;
}
参考:
点击打开链接
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 关于指针的一些事情
- MySQL 备份和恢复策略
- android上改变listView的选中颜色
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列