【问题标题】:How to read DATETIME with MySQL C++ Connector 8.0 with ISO 8601 format如何使用 ISO 8601 格式使用 MySQL C++ Connector 8.0 读取 DATETIME
【发布时间】:2022-12-11 00:10:59
【问题描述】:

我想使用 ISO 8601 格式的 MySQL C++ Connector 8.0 阅读DATETIME。 我已经阅读了很多示例,但它们似乎都太复杂了,其中一些无法正常工作。这样的一个非常复杂,似乎代码太多而且非常不稳定。 如果DATETIME包含微秒,有没有更好的方法来读取DATETIME

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

【问题讨论】:

    标签: c++ mysql mysql-connector


    【解决方案1】:

    是的。这是。

    这是代码。 DATETIME 被解释为数据类型 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;
    }
    

    这是理论

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

    【讨论】:

    • 你刚刚回答了你自己的帖子吗?
    • @Fyx 是的。如果我忘记了密码,请保存以备后用。
    • @Fyx 阅读日期时间似乎是 SO 的一个非常常见的问题。但是没有人像我一样解决了。
    • 好的,但是您是要编辑问题还是回答问题?
    • @Fyx 回答它。只是为了我以后记住它。
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 2015-03-22
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多