您的位置:首页 > 编程语言 > C语言/C++

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

2014-12-15 14:33 746 查看
在使用过程中,一定不要释放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++的使用文档,不知道是没有,还是什么情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: