问题
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