C++ OTL MySQL(Windows/Linux) V8.1
阅读原文时间:2023年07月13日阅读:1

Windows每秒钟10000条以上插入;Linux每秒插入300条以上。Q269752451

输出截图:

Linux输出:

Windows输出:

有须要的联系 QQ 3508551694

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

測试代码

main.cpp

int main(int, char *[])
{
vector arr_data;
print("begin make_data");

make\_data(arr\_data);  
print("begin insert 3000 data");

try  
{  
    Otlv4\_Handle otl\_handler;  
    otl\_handler.logon("root/123456@mysql\_from\_centos");  
    otl\_handler.insert(e\_eledata::insert\_sql(),arr\_data);  
    print("insert data end ");

    arr\_data.clear();  
    otl\_handler.select(e\_eledata::select\_sql(),arr\_data);  
    print("select top 10:");  
    for (int i=0;i<arr\_data.size();++i)  
    {  
        cout<<arr\_data\[i\].to\_string()<<endl;  
    }  
}  
catch(otl\_exception& ex)  
{  
    cout<<"exception:\\n"  
        <<"msg:"<<ex.msg<<"\\n"  
        <<"text:"<<ex.stm\_text<<"\\n"  
        <<"info:"<<ex.var\_info<<"\\n";  
}  

return 0;  

};

使用:

Windows下:直接打开project编译执行(填写一下username、password、ODBC数据源名称就可以)

Linux下:直接make。执行(填写一下username、password、ODBC数据源名称就可以)

版本号2.0简单封装OTL

头文件Otlv4_Handle.h

#ifndef OTLV4_HANDLE_H
#define OTLV4_HANDLE_H
// =================================================================================
// ORACLE, ODBC and DB2/CLI Template Library, Version 4.0.262,
// Copyright (C) 1996-2012, Sergei Kuchin (skuchin@gmail.com)
// modified by carea @ 2014-10-29 Version 1.0
// =================================================================================

/************************************************************************/
/* */
/* 邦定变量类型 | C++ 程序使用的相应类型 */
/* USED IN MySQL table | USED IN C++ STRUCT MEMBER TYPE */
/* int | long long */
/* timestamp | otl_datetime */
/* varchar | std::string */
/* bigint | long long */
/* decimal | double */
/* */
/************************************************************************/

//数据类型应该支持的输入输出操作
//otl_stream& operator>>(otl_stream& in ,T& t)
//otl_stream& operator<<(otl_stream& out ,const T& t)

/*********************************************************************************/
/* */
/* 经常使用SQL语句写法 */
/*otl_handle.select("select f2 from test_tab",arr); */
/*otl_handle.insert("insert into test_tab1(f2)values(:f2 )",arr); */
/* */
/*说明:数据库表中假设有属性名为:IP、PORT、INTERVAL这样的字段,赶紧改名!

!(与keyword冲突) */
/* */
/*********************************************************************************/

#ifdef OTLV4_HANDLE_WINDOWS
#define OTL_ODBC_MSSQL_2008
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
#define OTL_STL//using std::string
#define OTL_ANSI_CPP // Turn on ANSI C++ typecasts
#define OTL_BIGINT long long
#define OTL_STR_TO_BIGINT(str,n) { n=_atoi64(str); }
#define OTL_BIGINT_TO_STR(n,str) { _i64toa(n,str,10); }
#define INSERT_NUM 10000
#endif

#ifdef OTLV4_HANDLE_LINUX
#define OTL_ODBC_UNIX
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
//#define OTL_ODBC_MYSQL
#define OTL_STL//using std::string
#define OTL_ANSI_CPP // Turn on ANSI C++ typecasts
#define OTL_BIGINT long long
#define OTL_STR_TO_BIGINT(str,n) { n=atoll(str); }
#define OTL_BIGINT_TO_STR(n,str) { sprintf(str,"%lld", n);}
#define INSERT_NUM 1
#endif

#include "otlv4.h"// include the OTL 4.0 header file

#include
#include
#include
#include
using namespace std;

#ifndef SCP_EXPORT_H
#include "SCP_Export.h"
#endif

class SCP_Export Otlv4_Handle
{
public:
Otlv4_Handle();
bool logon(const string& db_info);
bool reconnect_to_db_once(void);
~Otlv4_Handle(void);
template int insert(const string& sql,const vector& data);
template int select(const string& sql,vector& data);
int count_table_field(const string& table_name,const string& field_name);
private:
otl_connect db; // connect object
string db_infor_;
};

template int Otlv4_Handle::insert(const string& sql,const vector& data)
{
db.auto_commit_off();
otl_stream out;
out.open(INSERT_NUM, //buffer size should be == 1 always on INSERT on Linux MySQL.
//buffer size can be bigger than 2000 on Windows MySQL.
sql.c_str(),
db); // connectobject
//submit
for (size_t i = 0 ; i < data.size(); i++)
{
out<<data[i];
}
out.flush();
db.commit();

return 0;  

}

template int Otlv4_Handle::select(const string& sql,vector& data)
{
//使用邦定变量类型
//sql = "select* from "+talbe;
db.auto_commit_off();
otl_stream in;
in.open(100, //buffer size
sql.c_str(),// SELECTstatement
db // connectobject
);

// copy all rows to be fetched into the vector  
copy(otl\_input\_iterator<T,ptrdiff\_t>(in),otl\_input\_iterator<T,ptrdiff\_t>(),back\_inserter(data));    

return 0;  

}

#endif

存储数据类定义

存储数据类头文件e_eledata.h

#ifndef E_ELEDATA_H
#define E_ELEDATA_H

#include
#include
#include
#include
using namespace std;

#ifndef SCP_TYPE_H
#include "scp_type.h"
#endif

#ifndef SCP_EXPORT_H
#include "SCP_Export.h"
#endif

#ifndef OTLV4_HANDLE_H
#include "Otlv4_Handle.h"
#endif

class SCP_Export e_eledata
{
public:
friend SCP_Export otl_stream& operator<<(otl_stream& out , const e_eledata& eledata); friend SCP_Export otl_stream& operator>>(otl_stream& in , e_eledata& eledata);
string to_string(void) const;
e_eledata();
~e_eledata();
e_eledata(const e_eledata& data);
e_eledata& operator=(const e_eledata& data);
int check_format(void);
static string insert_sql(void);
static string select_sql(void);
public:
id_type id;//64位机器这里不须要使用long,假设使用long则字节太长而打印不出来
id_type e_elemeterhead_id;
otl_datetime dldt;
double tae; //总有功电量。单位:kWh。

};

#endif

批量提交

因不同的数据库以及不同的操作系统,批量提交的buffer size有所不同,见官方演示样例代码:

Windows MySQL 能够设置大于10000

Linux MySQL 临时设置成1.

配置ODBC数据源

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">