添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using mysql-connector-cpp-8.0.18( https://github.com/mysql/mysql-connector-cpp )

Mysql result stores is variable with type mysqlx::Value https://dev.mysql.com/doc/dev/connector-cpp/8.0/classmysqlx_1_1abi2_1_1r0_1_1_value.html

CREATE TABLE `table_name` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `data_update` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; RowResult res = mysession.sql("SELECT id, data_update FROM table_name").execute(); Row row; while ((row = res.fetchOne())) { cout << "Id: " << row[0] << endl; // Ok std::string data_update = row[1]; // How to get datetime here? // row[1].getType() == Type::Document(8)

How should I work with DATETIME?

I found the answer on mysql forum

The solution is parsing raw data. It use protobuf base 128 variant

Simple example for decoding:

uint64_t DecodeOne(uint8_t * in , size_t * len) {
  uint64_t r = 0;
    uint64_t val = * in ;
    r = (r << 7) | (uint64_t)(val & 127); in ++;
    ( * len) --;
  } while (( * len > 0) && (uint64_t( * in ) & 128));
  return r;
void DecodeBuffer(uint8_t * buffer, size_t size) {
  std::vector < uint8_t > reverseBuffer;
  for (size_t i = 1; i <= size; i++) {
    reverseBuffer.push_back(buffer[size - i]);
  size_t tmpSize = size;
  uint8_t * reverseData = reverseBuffer.data();
  while (tmpSize > 0) {
    std::cout << DecodeOne( & reverseData[size - tmpSize], & tmpSize) << " ";
  std::cout << "\n";
mysqlx_get_bytes(row, idx, 0, data, size);
DecodeBuffer(data, size);
std::pair <
  const unsigned char * , size_t > res = value.getRawBytes();
DecodeBuffer(res.first, res.second);

But there is a bug in CPP API in mysql-connector-cpp-8.0.18.

CPP result doesnot contain seconds. So its possible to use only c api.

That bug was fixed in 8.0.20 (released 2020-04-27) dev.mysql.com/doc/relnotes/connector-cpp/en/news-8-0-20.html – Kira M. Backes Oct 19, 2022 at 14:53

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.