0

我正在尝试将字符串插入 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 似乎是唯一不起作用的东西。

PS我是boost.asio和野兽的新手。我很确定我只需要连接到 mysql 一次,但是在尝试调试我遇到的这个问题时尝试了两个连接。

如果有人有任何见解或建议,非常感谢,谢谢!

4

0 回答 0