尝试使用boost.beast插入mysql时出错

时间:2018-08-13 21:56:05

标签: c++ mysql server boost-asio boost-beast

我正在尝试将字符串插入mysql数据库并收到错误terminate called after throwing an instance of 'sql::SQLException' what(): Aborted (core dumped),然后再尝试将其插入数据库,因此我成功地从数据库中获取了一个表,所以我不明白为什么mysql不是当我尝试插入表格时喜欢它。下面是我的服务器代码。

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));
}

该代码一直运行到我注释过//~~~~~~~HERE~~~~~~~的位置,然后下面的行stmt1->executeQuery("INSERT INTO address (publicIP) VALUES ('"+temppubIP+"')");导致错误。但是,即使服务器崩溃,它仍然会将字符串temppubIP保存到mysql db表中。如果我注释掉INSERT INTO行,则当客户端在客户端上使用GET请求并接收数据库表时,服务器不会崩溃。因此,我的目标是使服务器运行,并且当客户端连接到服务器时,服务器获取数据库表,然后将字符串发布到数据库,然后将数据库表发送回客户端。 INSERT INTO似乎是唯一不起作用的东西。

P.S。我是boost.asio和beast的新手。我很确定我只需要连接到mysql一次,但是在尝试调试此问题时尝试了两个连接。

如果有人有任何见解或建议,我们将不胜感激,谢谢!

0 个答案:

没有答案