How to fetch DateTime from mysql using xdevapi

梦想与她 提交于 2021-02-07 09:26:53

问题


When I select a dateTime field, it gets returned as 6 bytes. How can I convert it to a time_point or struct tm?

mysqlx::Session session( "mysqlx://root:password@127.0.0.1:33060/catalog" );
auto row = session.sql( "select create_time from information_schema.tables order by 1 LIMIT 1" ).execute().fetchOne();
assert( row[0].getType()==8 );//raw type
assert( row[0].getRawBytes().second==6 );//6 bytes
var bytes = row[0].getRawBytes().first;
//e2 0f 08 0c 0a 32 
//2018-08-12 10:50:04

回答1:


Hey I just spend 5 hours tring to figure out the same thing, the solution is to project the TIME/DATE/DATETIME field as a UNIX timestamp (integer) in your SQL Statement using UNIX_TIMESTAMP().

Then you can easily get the field as time_t (and optionally convert to a struct tm).

#include<time.h>

mysqlx::Session session{"mysqlx://root:password@127.0.0.1:33060/catalog"};
auto row = session.sql("SELECT UNIX_TIMESTAMP(create_time) FROM information_schema.tables ORDER BY 1 LIMIT 1").execute().fetchOne();
time_t creationTime = (int) row[0];
struct tm* creationTimePoint = localtime(creationTime);

Hope this helps. -Minding



来源:https://stackoverflow.com/questions/52137654/how-to-fetch-datetime-from-mysql-using-xdevapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!