简体   繁体   English

如何在连接器 C++ 中将原始字节转换为字符串

[英]how to convert raw bytes to string in connector c++

I'm using connector c++ to making query to mysql through c++ and everything is okay except date type!我正在使用连接器 C++ 通过 C++ 对 mysql 进行查询,除了日期类型外,一切正常!

When I trying to print data resulted from the query, all columns are printed except those which are in the type of DATE, and instead of printing the date value they print <3 raw bytes> .当我尝试打印由查询产生的数据时,除了那些 DATE 类型的列外,所有列都被打印出来,而不是打印日期值,而是打印<3 raw bytes>

How can print that in the real form of date yyy-mm-dd instead of <3 raw bytes>.如何以日期 yyy-mm-dd 的真实形式而不是 <3 个原始字节> 打印它。

this is my code:这是我的代码:

#include <iostream>
#include <mysqlx/xdevapi.h>

using namespace std;
using namespace mysqlx;

template<typename type>
void print(const list<type>& lst) noexcept
{
    for (auto it{ lst.begin() }; it != lst.end(); it++)
    {
        try
        {
            int index{ 0 };
            while (true)
                cout << (*it)[index++] << '\t';
        }
        catch (...)
        {
            cout << endl;
        }
    }
}

int main(int argc, char** argv, char** pnv)
{
    Session dbms_session{
        SessionOption::HOST, "localhost",
        SessionOption::PORT, 33060,
        SessionOption::USER, "myusername",
        SessionOption::PWD, "mypassword",
        SessionOption::DB, "classicmodels",
        SessionOption::SSL_MODE, SSLMode::DISABLED
    };
    Schema classicmodels_schema{ dbms_session.getSchema("classicmodels") };

    Table employees_table{ classicmodels_schema.getTable("employees") };
    RowResult employees_full_name{ employees_table.select("firstName", "lastName").execute() };

    list<Row> employees_full_name_result{ employees_full_name.fetchAll() };

    Table members_table{ classicmodels_schema.getTable("members") };
    Table committees_table{ classicmodels_schema.getTable("committees") };

    const char* sql_command{
        "SELECT\n"
        "customers.customerNumber,\n"
        "customers.contactLastName,\n"
        "customers.contactFirstName,\n"
        "orders.orderDate,\n"
        "orders.status\n"
        "FROM customers\n"
        "INNER JOIN orders\n"
        "USING (customerNumber);\n"
    };

    RowResult databases{ dbms_session.sql(sql_command).execute() };

    list<Row> data{ databases.fetchAll() };
    print(data);
}

and this is my results:这是我的结果:

363     Young   Dorothy <3 raw bytes>   Shipped
128     Keitel  Roland  <3 raw bytes>   Shipped
181     Frick   Michael <3 raw bytes>   Shipped
121     Bergulfsen      Jonas   <3 raw bytes>   Shipped
141     Freyre  Diego   <3 raw bytes>   Shipped
145     Petersen        Jytte   <3 raw bytes>   Shipped
278     Rovelli Giovanni        <3 raw bytes>   Shipped
131     Lee     Kwai    <3 raw bytes>   Shipped
385     Cruz    Arnold  <3 raw bytes>   Shipped
486     Salazar Rosa    <3 raw bytes>   Shipped
187     Ashworth        Rachel  <3 raw bytes>   Shipped
129     Murphy  Julie   <3 raw bytes>   Shipped
144     Berglund        Christina       <3 raw bytes>   Shipped
124     Nelson  Susan   <3 raw bytes>   Shipped
172     Bertrand        Marie   <3 raw bytes>   Shipped
424     Hernandez       Maria   <3 raw bytes>   Shipped
use below command 
select date_format(<column name with type>,'%Y-%m-%d') from <table name>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM