2017-11-14 12 views

答えて

1

実際に使用するMSQL API関数についての詳細は提供していないため、この回答も一般的です。

あなたが作成し、次のAPI関数使用してポインタを削除する必要がある場合:

std::shared_ptr<API_OBJ> my_s_ptr(api_obj_create(...), 
    [](API_OBJ* p) { api_obj_delete(p); }); 

標準ライブラリは通常である:カスタム削除手段を提供する必要がstd::shared_ptrであなたのポインタを管理するには

API_OBJ *api_obj_create(...); 
api_obj_delete(API_OBJ*); 

をオーバーブーストライブラリーより好ましい。

1

ヘッダ

#include <boost/shared_ptr.hpp> 
#include <string> 
#include <vector> 
#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> 

sql::Driver *host_1_driver; 
boost::shared_ptr <sql::Connection> host_1_conn; 
boost::shared_ptr <sql::Statement> host_1_stmt; 
boost::shared_ptr <sql::ResultSet> host_1_res; 
boost::shared_ptr <sql::PreparedStatement> host_1_pstmt;   

bool reconnect_state = true;  
std::string host_1_name; 
std::string host_1_user_name; 
std::string host_1_password; 
std::string host_1_database_name; 

接続

host_1_driver = get_driver_instance(); // protected    
host_1_conn.reset(host_1_driver->connect (host_1_name, host_1_user_name, host_1_password)); // connect to mysql 
host_1_conn->setClientOption("OPT_RECONNECT", &reconnect_state);  
host_1_conn->setSchema(host_1_database_name); 

スレッド

void float_to_sql(std::string query, std::vector <float> data_to_write, std::vector <float> data_ids) 
{ 
    host_1_driver->threadInit(); // prevents multiple open connections 
    if (host_1_conn.get() == NULL) 
    { 
     ERROR << "host_1 connection is not open"; 
     throw -1; 
    }  
    host_1_pstmt.reset (host_1_conn->prepareStatement (query)); 
    for (int i = 0; i < (int) data_to_write.size(); i++) 
    { 
     host_1_pstmt->setDouble(1, data_to_write.at(i)); 
     host_1_pstmt->setInt(2, data_ids.at(i)); 
     host_1_pstmt->executeUpdate(); 
    }    
    host_1_driver->threadEnd();     
    return; 
} 

試験

std::string query = "update ErrorCodes set Val = ? where ID = ?"; 
std::vector <float> data {1, 1, 1}; 
std::vector <float> data_ids {3, 5, 9}; 
float_to_sql(query, data, data_ids); 
関連する問題