ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ insert into table in bulk

2022-05-15 00:02:47  阅读:202  来源: 互联网

标签:insert char NULL int into bulk sql table include


1.Create table in mysql

CREATE TABLE `mt` (
  `BookIndex` int NOT NULL AUTO_INCREMENT,
  `BookId` bigint NOT NULL,
  `BookName` varchar(100) NOT NULL,
  `BookTitle` varchar(100) NOT NULL,
  PRIMARY KEY (`BookIndex`)
) 
//ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

 

run below command to validate the table created statement.

desc mt;

 

 

 

 

2.Cpp code

#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <iostream>
#include <sstream>
#include <string.h>

using namespace std;

static char *uuidValue = (char *)malloc(40);

char *getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}

void batchInsert(int loops);

int main(int args, char **argv)
{
    batchInsert(atoi(argv[1]));
}

void batchInsert(int loops)
{
    try
    {
        sql::Driver *driver;
        sql::Connection *conn;
        sql::PreparedStatement *prepStmt;
        driver=get_driver_instance();
        
        conn=driver->connect("tcp://127.0.0.1:3306","root","Password");
        conn->setSchema("myDB");

        stringstream ss;
        ss<<"insert into mt(BookId,BookName,BookTitle) values ";
        for(int i=0;i<loops;i++)
        {
            ss<<"("<<i<<",'"<<getUuid()<<"','"<<getUuid()<<"'),"<<endl;
        }        

        string str=ss.str();
        int lastIndex=str.find_last_of(",");
        str=str.erase(lastIndex);
        prepStmt=conn->prepareStatement(str);
        prepStmt->execute();
        delete prepStmt;
        delete conn;
    }
    catch (sql::SQLException &e)
    {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
    cout<<"Finished in void batchInsert(int loops)!"<<endl;
}

 

3.Compile via g++ as below

g++ -g -std=c++2a -I. -Wall -I/usr/include/cppconn *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -L/usr/lib -lmysqlcppconn

 

 

4.Execute the compiled result via

time ./h1 100000;

 

 

 

The fact has illustrated that insert into table once in bulk will be much faster than insert looply one by one.

5.Validate in mysql via sql  

select * from mt;

 

标签:insert,char,NULL,int,into,bulk,sql,table,include
来源: https://www.cnblogs.com/Fred1987/p/16271985.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有