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

标签 c++ mysql c mysql-connector

我使用的是 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() 返回一个临时的 Session 对象。所有临时对象都在创建它们的语句结束时销毁。

由于 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

关于c++ - 关闭游标错误mysql连接器c++ 8.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203780/

相关文章:

c++ - Clion 体系结构 x86_64 的 undefined symbol

c++ - 当我没有调用两次时,输出如何显示两次?

php - 不使用 Flash/Silverlight 的 2 人游戏连接

php - 在index.php中输入的数据无法在process.php上显示

c - 了解逗号运算符

c++ - 生成库的多个实例的 Makefile

c++ - 成员函数指针的静态成员数组

c++ - 比较奇怪的行为

php - 使用 Laravel 创建 LMS 并向学生分配类(class)

c - 杀死系统调用C,权限