I am trying to insert a string into a mysql database and am getting the error terminate called after throwing an instance of 'sql::SQLException' what(): Aborted (core dumped)
Before I try insert into I'm getting a table from the database successfully so I don't understand why mysql isn't liking it when I try to insert into the table. Below is my server code.
void handle_request(http::request<Body, http::basic_fields<Allocator> > &&req, Send &&send) {
// Returns a bad request response
auto const bad_request = [&req](boost::beast::string_view why) {
http::response<http::string_body> res{ http::status::bad_request, req.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = why.to_string();
res.prepare_payload();
return res;
};
// Make sure we can handle the method
if (req.method() != http::verb::get)
return send(bad_request("Unsupported HTTP-method"));
// Request path must be absolute and not contain "..".
auto target = req.target();
if (target.empty() || target[0] != '/' || target.find("..") != boost::beast::string_view::npos)
return send(bad_request("Illegal request-target"));
std::string data = "";
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *resS;
sql::Driver *driver1;
sql::Connection *con1;
sql::Statement *stmt1;
sql::ResultSet *resS1;
// Create a connection
driver = get_driver_instance();
con = driver->connect("tcp://111.111.111.111:3306", "root", "password");
con->setSchema("server");
stmt = con->createStatement();
resS = stmt->executeQuery("SELECT * FROM `address`");
while(resS->next()){
data = data + " " + resS->getString("publicIP");
}
//parsing IP
int len = temppubIP.length();
while(temppubIP[len-1] != ':'){
temppubIP.pop_back();
len--;
}
temppubIP.pop_back();
driver1 = get_driver_instance();
con1 = driver1->connect("tcp://111.111.111.111:3306", "root", "password");
con1->setSchema("server");
stmt1 = con1->createStatement();
//~~~~~~~~~HERE~~~~~~~~~
stmt1->executeQuery("INSERT INTO address (publicIP) VALUES ('"+temppubIP+"')");
delete stmt;
delete con;
delete stmt1;
delete con1;
std::cout<<"t2"<<std::endl;
// std::string pubIP = boost::lexical_cast<std::string>
(socket.remote_endpoint());
//std::cout<<pubIP<<std::endl;
http::response<http::string_body> res{ http::status::ok, req.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.body() = data;
res.prepare_payload();
res.keep_alive(req.keep_alive());
std::cout<<"t4"<<std::endl;
return send(std::move(res));
}
The code works all the way up to where I have commented //~~~~~~~HERE~~~~~~~
Then the following line stmt1->executeQuery("INSERT INTO address (publicIP) VALUES ('"+temppubIP+"')");
causes the error. But even though the server crashes it still does save the string temppubIP
to the mysql db table. If i comment out the INSERT INTO line the server doesn't crash when a client uses a GET request on it and receives the database table. So my goal here is to have the server be running, and when a client connects to it the server gets the database table then posts a string to the database then sends the database table back to the client. The INSERT INTO seems to be the only thing not working.
P.S. I'm new to boost.asio and beast. I'm pretty sure I only need to connect to mysql once but tried two connections when trying to debug this issue I have.
If anyone has any insight or suggestions it is much appreciated, Thank You!