Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query executes but throws exception

Tags:

c++

mysql

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?

like image 482
lalli Avatar asked Aug 01 '11 19:08

lalli


1 Answers

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.

like image 192
Soul_man Avatar answered Oct 13 '22 19:10

Soul_man