10

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?

lalli
  • 6,083
  • 7
  • 42
  • 55

3 Answers3

24

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.

Soul_man
  • 575
  • 5
  • 15
  • 1
    +1 for the answer to a really old question. i've lost the code so can't test it right away. I'll test and accept the answer eventually, or if anyone else verifies it before me.. – lalli Mar 05 '13 at 08:59
0

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).

Lars Kokemohr
  • 590
  • 5
  • 6
  • 2
    Greetings. It is unfortunate that this answer has been flagged as low quality. The information you've given can probably be used to answer the questions asked, but you need to put those extra couple of sentences in. After you have answered these questions, I doubt this question will be deleted. "Is this common? Should I just ignore this?" – autistic Jan 20 '16 at 15:56
-1

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”.

HKTonyLee
  • 3,111
  • 23
  • 34
Jesse
  • 580
  • 4
  • 7