24,854
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <driver/mysql_connection.h>
#include <driver/mysql_driver.h>
#include <cppconn/prepared_statement.h>
#pragma comment(lib,"mysqlconn/debug/mysqlcppconn.lib")
sql::Connection* g_conn = NULL;
void InitDB()
{
sql::Driver* driver = get_driver_instance();
sql::ConnectOptionsMap connection_properties;
connection_properties["hostName"] = std::string("tcp://192.168.10.10:3306");;
connection_properties["userName"] = std::string("admin");;
connection_properties["password"] = std::string("admin");;
connection_properties["schema"] = std::string("tcr_db");;
connection_properties["OPT_RECONNECT"] = true;
g_conn = driver->connect(connection_properties);
g_conn->setAutoCommit(0);
}
sql::ResultSet* Query(const char *szSql)
{
try
{
sql::PreparedStatement* prepStatement = g_conn->prepareStatement(szSql);
if (prepStatement)
{
return prepStatement->executeQuery();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;;
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
InitDB();
sql::ResultSet* pReSet1 = Query("CALL CheckHandleTask()");
if (pReSet1)
{
while (pReSet1->next())
{
std::cout << " 查询结果:" << pReSet1->getUInt("HandleID_") << std::endl;
}
}
delete pReSet1;
pReSet1 = NULL;
// 以下再进行调用 就会抛出 Commands out of sync; you can't run this command now 的异常。
sql::ResultSet* pReSet2 = Query("CALL test()");
if (pReSet2)
{
while (pReSet2->next())
{
std::cout << " 查询结果:" << pReSet2->getUInt("FileID_") << std::endl;
}
}
delete pReSet2;
pReSet2 = NULL;
getchar();
return 0;
}