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 )