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

f87krz0w  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(194)

我试图插入一个字符串到一个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请求并接收数据库表时,服务器不会崩溃。因此,我的目标是让服务器运行,当客户机连接到服务器时,服务器获取数据库表,然后向数据库发送字符串,然后将数据库表发送回客户机。插入似乎是唯一不起作用的东西。
p、 我是新来的。阿西奥和野兽。我很确定我只需要连接到mysql一次,但是在调试这个问题时尝试了两次连接。
如果有人有任何见解或建议,我们将不胜感激,谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题