如何使用mysql连接器c++; 如何使用MySQL连接器C++设置自动重定向选项? (不适用于mysql c api)
我不是这个库的用户,所以我对它的了解只值最后10分钟,所以请验证一下 一般来说,有关库的各种特定细节的使用情况的此类信息的最佳来源是查看它的单元测试。OSS最棒的地方 因此,如果您查看可以在源代码树上找到的MySQL连接器/C++单元测试,您将看到下面的摘录如何使用mysql连接器c++; 如何使用MySQL连接器C++设置自动重定向选项? (不适用于mysql c api),c++,mysql,database,database-connection,C++,Mysql,Database,Database Connection,我不是这个库的用户,所以我对它的了解只值最后10分钟,所以请验证一下 一般来说,有关库的各种特定细节的使用情况的此类信息的最佳来源是查看它的单元测试。OSS最棒的地方 因此,如果您查看可以在源代码树上找到的MySQL连接器/C++单元测试,您将看到下面的摘录 sql::ConnectOptionsMap connection_properties; ... connection_properties["OPT_RECONNECT"]=true; try { con.reset(dri
sql::ConnectOptionsMap connection_properties;
...
connection_properties["OPT_RECONNECT"]=true;
try
{
con.reset(driver->connect(connection_properties));
}
catch (sql::SQLException &e)
{
std::cerr << e.what();
}
话虽如此,mysql中的重新连接选项必须非常小心地使用,因为您必须重置任何会话变量等。您必须将重新连接的连接视为全新的连接。这必须通过您正在使用的特定版本MySQL的文档进行验证。您需要通过引用传递布尔值。我的代码是:
bool myTrue = true;
con->setClientOption("OPT_RECONNECT", &myTrue);
这对我很有用。一个更完整的例子 标题
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
std::string host_name = "localhost";
std::string user_name = "user1234";
std::string password = "pw1234";
std::string database_name = "TestingDB";
bool reconnect_state = true;
sql::ConnectOptionsMap connection_properties;
sql::Driver *driver;
boost::shared_ptr <sql::Connection> con;
boost::shared_ptr <sql::Statement> stmt;
boost::shared_ptr <sql::ResultSet> res;
boost::shared_ptr <sql::PreparedStatement> pstmt;
线程
std::vector <std::string> database::string_from_sql (std::string query, std::string column_name)
{
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;
std::vector <std::string> svec;
try
{
driver->threadInit(); // prevents multiple open connections
if (con.get() == NULL)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | connection is not open" << std::endl;
throw -2;
}
stmt.reset (con->createStatement ());
res.reset (stmt->executeQuery (query));
while (res->next())
{
svec.push_back(res->getString (column_name));
}
driver->threadEnd();
}
catch (sql::SQLException &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what(): " << e.what() << " (MySQL error code: " << e.getErrorCode() << ", SQLState: " << e.getSQLState() << " )" << std::endl;
throw -1;
}
if (svec.empty())
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | return vector size is 0 (Empty set)" << std::endl;
throw -3;
}
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;
return svec;
}
std::vector数据库::string\u from\u sql(std::string查询,std::string列名称)
{
std::C API中的常见做法是,如果查询失败,请ping数据库并重试查询。谢谢@CodeMedic,我的库版本没有con.reset方法,我将使用最新版本进行尝试。我还尝试了con->setClientOption(“OPT_RECONNECT”,“true”),但没有成功:(说应该是MYSQL\u OPT\u RECONNECT
。你确定吗?gerrytan:cpp连接器在内部使用MYSQL\u OPT\u RECONNECT,但要触发它,应该使用OPT\u RECONNECT。@VladimirShutow:我相信这适用于所有选项?
driver = get_driver_instance (); // protected
con.reset(driver->connect (host_name, user_name, password)); // connect to mysql
con->setClientOption("OPT_RECONNECT", &reconnect_state);
con->setSchema(database_name);
std::vector <std::string> database::string_from_sql (std::string query, std::string column_name)
{
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;
std::vector <std::string> svec;
try
{
driver->threadInit(); // prevents multiple open connections
if (con.get() == NULL)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | connection is not open" << std::endl;
throw -2;
}
stmt.reset (con->createStatement ());
res.reset (stmt->executeQuery (query));
while (res->next())
{
svec.push_back(res->getString (column_name));
}
driver->threadEnd();
}
catch (sql::SQLException &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what(): " << e.what() << " (MySQL error code: " << e.getErrorCode() << ", SQLState: " << e.getSQLState() << " )" << std::endl;
throw -1;
}
if (svec.empty())
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | return vector size is 0 (Empty set)" << std::endl;
throw -3;
}
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;
return svec;
}