c++ - help - sqlite3 new database
Sqlite:如何綁定和從C++插入日期? (2)
使用C ++(Visual Studio)和sqlite。 如何將日期綁定到參數?
sqlite3_stmt *statement;
const char *sql =
"INSERT INTO employees "
"(full_name,"
"date_started)"
" VALUES "
"(@full_name,"
"@date_started)";
sqlite3_prepare_v2(database_, sql, -1, &statement, NULL);
int parameterIndex = sqlite3_bind_parameter_index(statement, "@full_name");
sqlite3_bind_text(statement, parameterIndex, "John Smith", -1, SQLITE_TRANSIENT);
parameterIndex = sqlite3_bind_parameter_index(statement, "@date_started");
// <??? what goes here ???>
// I want to include the local current time, so I want to know:
// 1. what's the best way to get local time in C++
// 2. and what goes here for the date binding
sqlite3_step(statement);
sqlite3_finalize(statement);
注意:我不想使用sql設置當前時間(例如,CURRENT_TIMESTAMP等)
您應該能夠使用localtime()
函數獲取當地時間。
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
有關詳細信息和示例,請參閱http://www.cplusplus.com/reference/clibrary/ctime/localtime/ 。
沒有竅門:
const char * sql =
"INSERT INTO Employees(full_name, data_started) VALUES (?, ?)";
time_t time = 0x3DE43B0C;
sqlite3_bind_int64(statement, 2, time);
這裡是文檔的相關部分:
1.2日期和時間數據類型
SQLite沒有專門用於存儲日期和/或時間的存儲類。 相反,SQLite的內置日期和時間函數能夠將日期和時間存儲為TEXT,REAL或INTEGER值:
- TEXT作為ISO8601字符串(“YYYY-MM-DD HH:MM:SS.SSS”)。
- 真正的Julian日數,這是自公元前4714年11月24日格林威治中午以來的天數,根據公曆日曆。
- INTEGER as Unix Time,自1970-01-01 00:00:00 UTC以來的秒數。
應用程序可以選擇以這些格式存儲日期和時間,並使用內置的日期和時間功能在格式間自由轉換。