Hur kan jag hitta DATETIME från MySQL igenom MySQL/Connector C++ 8.0?
Detta är ett litet svårt problem.
MySQL/Connector C++ 8.0 är den senaste MySQL versionen där MySQL/Connector C är utgående.
För att läsa en tabell i en MySQL Databas så kan man göra så här:
std::vector<std::vector<std::string>> getDatabaseValues(const char tableName[]) {
std::vector<std::string> values;
std::vector<std::vector<std::string>> table;
if (isConnectedToDatabase()) {
// Select only the first row
std::string query = "SELECT * FROM " + std::string(tableName);
mysqlx::SqlResult result = connection->sql(query).execute();
int columnCount = result.getColumnCount();
if (result.hasData()) {
mysqlx::Row row;
while (row = result.fetchOne()) {
for (int i = 0; i < columnCount; i++) {
switch (row[i].getType()) {
case mysqlx::common::Value::UINT64:
values.push_back(std::to_string(row[i].get<uint64_t>()));
break;
case mysqlx::common::Value::INT64:
values.push_back(std::to_string(row[i].get<int64_t>()));
break;
case mysqlx::common::Value::FLOAT:
values.push_back(std::to_string(row[i].get<float>()));
break;
case mysqlx::common::Value::STRING:
values.push_back(row[i].get<std::string>());
break;
case mysqlx::common::Value::RAW:
mysqlx::bytes bytes = row[i].getRawBytes();
??? Nu då?
break;
}
}
table.push_back(values);
}
}
}
return table;
}
Problemet som jag har är att tillfället case mysqlx::common::Value::RAW: körs när jag läser datatypen DATETIME i MySQL. Detta har med att bytes är alltså
https://dev.mysql.com/doc/dev/connector-cpp/8.0/classmysqlx_1...
JSON data is represented as a JSON string. ENUM values are represented as strings with enum constant names. Values of type DATE and TIMESTAMP use the same representation as DATETIME, with time part empty in case of DATE values. GEOMETRY values use the internal geometry storage format described here.
Note that raw representation of BYTES and STRING values has an extra 0x00 byte added at the end, which is not part of the originial data. It is used to distinguish null values from empty byte sequences.
Så detta betyder att bytes har null-terminering samt att bytes kan både vara DATE och TIMESTAMP.
Men då är frågan. Hur får jag bytes till std::string i C++?
Datat i bytes ser ut så här under körning

Där riktiga värdet skall vara: '2022-12-04 12:31:29.285'
Bytevärdet är: 0x20c4aa83da0