Compile with cppconn and mysqlclient

1. cpp 

#include "Model/Util.h"
#include <functional>
#include <string_view>
#include <experimental/source_location>
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
#include <mysql/mysql.h>

void mysqlClient74();

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", "Root0001!");
        conn->setSchema("myDB");

        stringstream ss;
        int repeatedTimes = 1000000;
        int64_t num = 0;
        Util ul;

        for (int i = 0; i < loops; i++)
        {
            ss = stringstream();
            conn = driver->connect("tcp://127.0.0.1:3306", "root", "Root0001!");
            conn->setSchema("myDB");
            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);
            prepStmt = conn->prepareStatement(str);
            prepStmt->execute();
            cout << ul.getTimeNow() << ",Loop=" << i + 1 << " finished!" << endl;
            conn->close();
            prepStmt->close();
        }

        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;
        return;
    }
    cout << "Finished in void batchInsert(int loops)!" << endl;
}

 

 

2.Compile:

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

 

3.Execute

./h1 10

 

 

 

 

posted @ 2022-05-15 22:50  FredGrit  阅读(19)  评论(0编辑  收藏  举报