关闭游标错误 mysql 连接器 c++ 8.0

Closed cursor error mysql connector c++ 8.0

我正在使用 mysql 连接器 8.0,下面的代码在尝试获取行时抛出 Closed cursor 错误。

        std::string query = "SELECT `id`, `username`, `password`, `gender`, `email`, `group_id`, `state`, `unban_time`, `expiration_time`, `last_login`, `last_ip`, `birth_date`, `character_slots`, `pincode`, `pincode_expiry` FROM `game_account` WHERE username = ?;";

        mysqlx::RowResult res = server->get_mysql_session().sql(query).bind(username).execute();

        try {
            mysqlx::Row record = res.fetchOne();

错误:

CDK Error: get_rows: Closed cursor

server->get_mysql_session() returns 临时 Session 对象。所有临时对象都在创建它们的语句结束时销毁。

由于会话在您调用 fetchOne 之前被销毁,因此它失败了。

例如这段代码:

#include <iostream>
#include <string>

struct A
{
    A() { std::cout << "A()\n"; }
    ~A() { std::cout << "~A()\n"; }
};

std::ostream& operator << (std::ostream& os, const A& a) { os << "\nA<<"; return os; }

int main()
{
    std::cout << "line1\n";
    std::cout << "line2" << A() << "\n";
    std::cout << "line3\n";
}

产生以下输出:

line1
A()
line2
A<<
~A()
line3