2017-12-08 12 views
2

私はライブラリとしてMySQL Connector/C++を使用してMySQLデータベースから結果を得ています。私はC++ 11標準を使用しています。MySQL Connector/C++を使用してDATETIMEを取得し、time_tに保存する方法は?

#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に割り当てる:私は、データベースから(jointimeという名前)DATETIMEフィールドを取得し、フォローでtime_t変数としてそれを保存したいですか?

getDateTime()または関連する方法がありません。 stringとしてDATATIMEを返し++

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

答えて

0

のMySQLコネクタ/ C:

0

はこのような何かを試してみてください。出力形式は、あなたがtime_tにこの文字列を変換するには、次の機能を記述する必要が%Y-%m-%d %H:%M:%Sです:

time_t String2time_t(const string& strDateTime){ 
    tm t; 
    strptime(strDateTime.c_str(), "%F %T", &t); 
    return mktime(&t); 
} 

DATETIMEを取得し、time_tでそれを保存するには、この行を使用します。

time_t jt = String2time_t((string)res->getString("jointime")); 

に留意されたいです。 getStringの出力はSQLstringではなくstd::stringなので、書いた関数に渡す前にstd::stringに変換する必要があります。

+0

@ThomasSmyth tanks、私は新しく、それをブロックで書きましたが、それはそのように発行されました。 – Raphael

+0

'res'オブジェクトからアクセス可能なdatetimeをデータベースから読みたいと思います。あなたはこの物体をこれまでに使用したことはありません! –

関連する問題