• Mysqlclient insert into table in bulk


    1.h1.cpp

    #include <chrono>
    #include <iostream>
    #include <mysql/mysql.h>
    #include <sstream>
    #include <string.h>
    #include <uuid/uuid.h>
    
    using namespace std;
    
    static char *uuidValue = (char *)malloc(40);
    
    void mysqlClientDemo();
    void mysqlClientInsert2();
    void insertBulk3(int loops);
    
    char *getUuid()
    {
        uuid_t newUUID;
        uuid_generate(newUUID);
        uuid_unparse(newUUID, uuidValue);
        return uuidValue;
    }
    
    int main(int args, char **argv)
    {
        insertBulk3(atoi(argv[1]));
    }
    
    void insertBulk3(int loops)
    {
        try
        {
            MYSQL *conn, mysql;
            int state;
    
            mysql_init(&mysql);
            conn = mysql_real_connect(&mysql, "localhost", "root", "Root0001!", "myDB", 0, 0, 0);
    
            if (conn == NULL)
            {
                cout << mysql_error(&mysql) << endl;
                return;
            }
    
            stringstream ss;
            int64_t num = 0;
            int repeatedTimes = 1000000;
            chrono::time_point<chrono::system_clock> startTime;
            chrono::time_point<chrono::system_clock> endTime;
            for (int i = 0; i < loops; i++)
            {
                startTime = chrono::system_clock::now();
                ss = stringstream();
                ss << "insert into mt(BookId,BookName,BookTitle) values ";
                for (int j = 0; j < repeatedTimes; j++)
                {
    
                    ss << "(" << ++num << ",'" << getUuid() << "','" << getUuid() << "')," << endl;
                }
    
                string str = ss.str();
                int lastIndex = str.find_last_of(",");
                str = str.erase(lastIndex);
                state = mysql_query(conn, str.c_str());
                endTime = chrono::system_clock::now();
                if (state != 0)
                {
                    cout << "Num=" << num << " failed!" << endl;
                    return;
                }
                cout << "Loops=" << i + 1 << ",Num=" << num << " succeeded!,time cost:"
                     << chrono::duration_cast<chrono::seconds>(endTime - startTime).count() << " seconds,"
                     << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " milliseconds,"
                     << chrono::duration_cast<chrono::microseconds>(endTime - startTime).count() << " microseconds,"
                     << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!" << endl
                     << endl;
            }
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what() << '\n';
        }
    }

    2.Compile

    g++ -g -std=c++2a -I. -Wall -I/usr/include/mysql/ *.cpp -o h1 -L/usr/lib -lmysqlclient -luuid;
    g++ -g -std=c++2a -I. -I/usr/include/lib *.cpp -o h1 -lmysqlclient -luuid;
    g++ -g -std=c++2a -I. *.cpp -o h1 -lmysqlclient -luuid;

    3.Execute

    time ./h1 100;

  • 相关阅读:
    第07组 Alpha冲刺(1/6)
    第07组 团队Git现场编程实战
    第07组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    0012---求滑动距离
    0011---绝对值函数
    0010---温度转换
    0009---乘法问题
    0008---三位数倒序问题
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16275307.html
Copyright © 2020-2023  润新知