-2

I want to read DATETIME with MySQL C++ Connector 8.0 with ISO 8601 format. I have read lots of examples, but all of them seems to be too complex and some of them won't work. Such this one is very complex and seems to be to much code and very unstable. Is there any better way to read DATETIME if DATETIME contains microseconds?

Interpreting mysql connector c++ field Value's raw 4 bytes to a date

euraad
  • 2,467
  • 5
  • 30
  • 51

1 Answers1

0

Yes. It is.

Here is the code. DATETIME is intepreted as the data type RAW.

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 data = row[i].getRawBytes();
                        const mysqlx::byte* first = data.begin();
                        int lengthOfData = data.length();
                        switch (lengthOfData) {
                        case 10: // DATETIME(6)
                            int year = (first[1] << 7) | (first[0] & 0x7f);
                            int month = first[2];
                            int date = first[3];
                            int hour = first[4];
                            int minute = first[5];
                            int second = first[6];
                            int microsecond = (first[9] << 14) | (first[8] << 7) | (first[7] & 0x7f);
                            char text[20];
                            sprintf(text, "%i-%i-%i %i:%i:%i.%i", year, month, date, hour, minute, second, microsecond);
                            values.push_back(text);
                            break;
                        }
                        
                        
                    }
                }
                table.push_back(values);
                values.clear();
            }
        }
    }
    return table;
}

Here is the theory

enter image description here

https://dev.mysql.com/doc/dev/mysql-server/latest/group__MY__TIME.html#datetime_and_date_low_level_rep

euraad
  • 2,467
  • 5
  • 30
  • 51