Closed cursor error mysql connector c++ 8.0

情到浓时终转凉″ 提交于 2019-12-25 03:12:49

问题


I'm using mysql connector 8.0 and the below code throws a Closed cursor error when trying to fetch the row.

        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();

Error:

CDK Error: get_rows: Closed cursor

回答1:


server->get_mysql_session() returns a temporary Session object. All temporaries are destroyed at the end of the statement in which they were created.

As the session is destroyed before you call fetchOne it fails.

For example this code:

#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";
}

produces the following output:

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


来源:https://stackoverflow.com/questions/54203780/closed-cursor-error-mysql-connector-c-8-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!