My code:
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "123456");
stmt = con->createStatement();
stmt->executeQuery("CREATE USER 'user22'");
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
Creates the user22 but also throws an (empty?) exception:
# ERR: (MySQL error code: 0, SQLState: 00000 )
Of course, re-executing it results in proper exception:
# ERR: Operation CREATE USER failed for 'user22'@'%' (MySQL error code: 1396, SQLState: HY000 )
Commenting the executeQuery
line results in no exceptions (blank output)
Is this common? Should I just ignore this?
You are using executeQuery, which supposed to return sql::ResultSet object, and you are quering "CREATE USER" which returns true or false.
There is an execute method for such cases. So...
stmt = con->createStatement();
stmt->execute("CREATE USER 'user22'");
won't throw an error.
Yeah, i know that the post is 2 years old, but if someone bumps in to the same problem in future could be of use to them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With