1

I modified a set of code from MySQL page to test input data into MySQL table 'list'. The program exits with the following error

terminate called after throwing an instance of 'sql::SQLException'
  what():  
Aborted

After I checked through the table, the data was successfully inserted somehow. I modified this program to repeat inserting the data using a for loop but end up the loop exited after first iteration (program forced to exit during first insertion).

I also tried with hard-coded query, which worked in MySQL manually input, but still had the same issue.

#include <stdlib.h>
#include <iostream>
#include <string>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_driver.h>

using namespace std;

int main(void){
        cout << endl;
        cout << "Running test insert"  << endl;

        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("localhost", "dev", "1234");

        /* Connect to the MySQL test database */
        con->setSchema("million");

        string data("abc1234567890");

        stmt = con->createStatement();
        res = stmt->executeQuery("INSERT INTO list VALUES('0','" +
            data + "','" + data +"','" + data + "','" + data + "','" +     data + "');");
        }
        // replace with your statement
        delete res;
        delete stmt;
        delete con;

        cout << endl;

        return EXIT_SUCCESS;

}

With this try{} catch

try{....}
catch (sql::SQLException &e) {
    cout << "# ERR: SQLException in " << __FILE__;
    cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;     
    cout << "# ERR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << " )" << endl;}

Output this error message

# ERR: SQLException in test.cpp(main) on line 54
# ERR:  (MySQL error code: 0, SQLState: 00000 )
3
  • WARNING: Do not forget to properly escape your values. What you've got here is one step away from a severe SQL injection bug. Be sure to use prepared statements when adding data to your queries. Commented Mar 3, 2015 at 15:35
  • I recommend formatting your SQL query into a string before placing into the function. This will allow you to use a debugger and examine the contents of the string before it is sent to the MySQL database. Commented Mar 3, 2015 at 16:58
  • I got the same problem, any updates? Thanks. Commented Jan 26, 2016 at 2:39

2 Answers 2

2

The sql::SQLException is thrown because the executeQuery() function is supposed to return a sql::ResultSet after the SQL query execution (e.g. SELECT statement). For SQL queries that does not return any DB selection (e.g. CREATE, UPDATE) there is another function execute().

The original answer can be found here: MySQL Query executes but throws exception

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same problem and come across this post. So just for the records. I did an

if (res) {
  doSomeThing();
}

after executing the statement and the SQLException disappeared

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.