开发者

MySQL Query executes but throws exception

My code:

try {
    sql::Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;

    /* Create a connection */
    dri开发者_JS百科ver = 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.


I recently had the situation that a query resulted in an exception with error code 0 and sql status 1000. It's probably not the issue you had, but in case someone stumbles across this thread while trying to figure out something similar here's what solved it for me:

The whole reason I kept getting this error was that the log files written by the Java connector (the client-side slow query log and the performance metrics log) were created by the wrong user so that my process wasn't allowed to overwrite them. To be fair this resulted in a warning during startup, but I didn't expect an exception every time the connector tried to write to the log.

So this would be an example where an exception with error code 0 can in fact be seen as a warning that could be ignored (like Jesse's answer quoted from the MySQL reference).

By the way: status code 0 means successful completion (just like MySql error code 0) and status code 1000 means "warning" (https://docs.oracle.com/cd/F49540_01/DOC/server.815/a58231/appd.htm).


From MySQL Reference -- 13.6.7.2 DECLARE ... HANDLER Syntax:

An SQLSTATE value (a 5-character string literal) or a MySQL error code (a number). You should not use SQLSTATE value '00000' or MySQL error code 0, because those indicate success rather than an error condition. For a list of SQLSTATE values and MySQL error codes, see Section C.3, “Server Error Codes and Messages”.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜