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語句能夠插入當前時間。

相關代碼如下:

		//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;
}


參考:

點擊打開鏈接





發佈了24 篇原創文章 · 獲贊 7 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章