Reputation: 3247
I'm new to MySQL, also their C++ API and I'm having a trouble executing multiple queries at once instead of calling the same function twice, I mean my queries are kinda linked and logically they should be executed at once.
I used to do so for example
sql::ResultSet* sqlExecute(std::string Query)
try
{
sql::ResultSet *res;
res = statement->executeQuery(Query);
return res;
}
catch (sql::SQLException& e)
{
if (e.getErrorCode())
outc("%c%s: [SQL %c%d%c]%c %s\n", c_gray, my_time("WARN"), c_dark_purple, e.getErrorCode(), c_gray, c_dark_red, e.what());
return 0;
}
and call it like this (twice)
sqlExecute("alter table ###.players AUTO_INCREMENT = 1;");
sqlExecute("insert into ###.players (name, username, password) values('Random Name', 'imrandom', 'this should be random too');");
but when I try to execute them with just one function call separated by ;
sqlExecute("alter table ###.players AUTO_INCREMENT = 1;insert into ###.players (name, username, password) values('Random Name', 'imrandom', 'this should be random too');");
I get exception
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into ###.players (name, username, password) values('Random Name', 'imrand' at line 1
In MySQL Workbench, I can execute multiple queries at once. Why not in the API too?
Upvotes: 0
Views: 1386
Reputation: 51904
The ability to execute multiple statements separated by semicolons depends on the CLIENT_MULTI_STATEMENTS
connection property being enabled:
sql::ConnectOptionsMap options;
options["CLIENT_MULTI_STATEMENTS"] = true;
sql::mysql::MySQL_Driver *driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
sql::Connection *con = driver->connect(options);
Upvotes: 3