关闭游标错误mysql connector c ++ 8.0

时间:2019-01-15 17:13:21

标签: 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

1 个答案:

答案 0 :(得分:2)

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