c++ - Sqlite:如何从 C++ 绑定(bind)和插入日期?

标签 c++ sql sqlite datetime insert

使用 C++ (Visual Studio) 和 sqlite。如何将日期绑定(bind)到参数?

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等)

最佳答案

没有什么技巧:

const char * sql =
    "INSERT INTO Employees(full_name, data_started) VALUES (?, ?)";
time_t time = 0x3DE43B0C;
sqlite3_bind_int64(statement, 2, time);

这里是 documentation 的相关部分:

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

关于c++ - Sqlite:如何从 C++ 绑定(bind)和插入日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183761/

相关文章:

MySQL Group by week number a/multiple date 列

sqlite - 如何确定 SQLite 索引是否唯一? (使用 SQL)

c++ - C++结构可以有成员函数吗?

mysql - 使用有效但失败,为什么?

sql - 删除oracle sql中的换行符

ios - 在 NSFetchRequestController 中使用带有复杂 NSPredicate 的 NSFetchRequest

python - 如何从字典列表中修改字典的值

c++ - 如何从 sk_buff 获取 IP 选项?

c++ - 如何以非常初级的水平在 C++ 中显示四分之一圆?

c++ - 从 -30 到 30