MySQL Connector/C++ 使用中出现的一个错误

在使用过程中,一定不要释放sql:driver,因为这是一个全局的,你释放以后,下一次重新连接的时候,必然要出段错误。

#include <stdlib.h>  
#include <iostream>  
#include "mysql_connection.h"  
#include <boost/shared_ptr>
#include <cppconn/driver.h>  
#include <cppconn/exception.h>   
using namespace std;  
  
int main(void)  
{  
cout << endl;  
cout << "Let's have MySQL count from 10 to 1..." << endl;  
  
try {  
  sql::Driver *driver;  
  sql::Connection *con;  
 
  sql::ConnectOptionsMap  connection_properties;
   connection_properties["hostName"] = std::string ( host_ );
   connection_properties["userName"] = std::string ( user_ );
   connection_properties["password"] = std::string ( pawd_ );
   connection_properties["schema"]   = std::string ( db_ );
   <span style="color:#FF6666;">boost::shared_ptr<mysql::MySQL_Driver> driver_(sql::mysql::get_mysql_driver_instance());</span>
   boost::shared_ptr<sql::Connection> conn_(driver_->connect ( connection_properties ));
   }
   catch ( SQLException &e )
   {
        LOG_ERROR<<"数据库 连接异常: "<<e.what();
   }
   catch ( std::runtime_error &e )
   {
        LOG_ERROR<<"数据库 运行时异常: "<<e.what();
   }
}


红色的这一行,在实际项目中是有问题的,不能用智能指针来释放。在你的程序中,你根本就不需要释放这个 mysql::MySQL_Driver。我一直都没有找到MySQL Connector/C++的使用文档,不知道是没有,还是什么情况。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值