如何使用MySQL Connector/C++获取日期时间和存储到时间?

如何使用MySQL Connector/C++获取日期时间和存储到时间?,c++,mysql,c++11,datetime,C++,Mysql,C++11,Datetime,我使用MySQL Connector/C++作为库从MySQL数据库中获取结果。我使用的是C++11标准。我想从数据库中获取一个名为jointime的DATETIME字段,并将其存储为以下时间变量: #include <cstdlib> #include <iostream> #include <ctime> #include "mysql_connection.h" #include <cppconn/driver.h> #include <

我使用MySQL Connector/C++作为库从MySQL数据库中获取结果。我使用的是C++11标准。我想从数据库中获取一个名为jointime的DATETIME字段,并将其存储为以下时间变量:

#include <cstdlib>
#include <iostream>
#include <ctime>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
    auto driver = sql::mysql::get_mysql_driver_instance();
    auto con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
    con->setSchema("mydb");
    auto stmt = con->createStatement();

    auto res = stmt->executeQuery("SELECT * from users;");
    while (res->next()) {
        string username = res->getString("username");
        time_t jt ; // res->get???
        ...
    }
    delete res;
    delete stmt;
    delete con;
}
我应该如何获取jointime并将其分配给time\t jt


没有getDateTime或relative方法。

请尝试以下方法:

Time var = new Time();
jt = var.time();

MySQL连接器/C++以字符串形式返回数据时间。输出格式为%Y-%m-%d%H:%m:%S,因此需要编写以下函数以将此字符串转换为时间:

现在,使用此行获取DATETIME并将其保存在time\t中:


请注意,getString的输出是SQLstring而不是std::string,因此在传递到编写的函数之前,需要将其转换为std::string。

@thomass,我是新手,我在块中编写了它,但它是以这种方式发布的。我想从数据库中读取datetime,该数据库可以从res对象访问。你从来没有用过这个东西!
time_t String2time_t(const string& strDateTime){
    tm t;
    strptime(strDateTime.c_str(), "%F %T", &t);
    return mktime(&t); 
}
time_t jt =  String2time_t((string)res->getString("jointime"));