Linux C++ mysql create table and insert data into table,mysqlconnector set global net_read_timeout=3600

1.Install mysqlconnector;

sudo apt-get install libmysqlcppconn-dev;

2.Login on mysql and set timeout

sudo mysql -u root -p;

//3600 seconds
SET @@global.net_read_timeout=3600;

3.Util.h

//Util.h

#ifndef Util_H
#define Util_H

#include <chrono>
#include <ctime>
#include <fstream> 
#include <iostream>
#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>
#include <sstream>
#include <stdlib.h>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>

using namespace std;

class Util
{
public:
    static char *uuidValue;
    static char *dtValue;
    char *getTime();
    char *getUuid(); void mysql2(int len);
};

#endif

4.Util.cpp

#include "Model/Util.h"

char *Util::dtValue = (char *)malloc(20);
char *Util::uuidValue = (char *)malloc(40);

void Util::mysql2(int len)
{
    try
    {
        sql::Driver *dvr;
        sql::Connection *conn;
        sql::Statement *stmt;
        sql::PreparedStatement *pstmt;

        dvr = get_driver_instance();
        conn = dvr->connect("tcp://127.0.0.1:3306", "root", "Password");
        conn->setSchema("db");

        stringstream ss;
        uint64_t num = 0;

        for (int i = 0; i < len; i++)
        {
            ss = stringstream();
            if (conn->isClosed())
            {
                dvr = get_driver_instance();
                conn = dvr->connect("tcp://127.0.0.1:3306", "root", "Password");
conn->setSchema("db"); } stmt = conn->createStatement(); ss << "drop table if exists mt" << i << ";"; stmt->execute(ss.str()); ss = stringstream(); ss << "create table if not exists mt" << i << "(BookIndex int not null auto_increment,BookId bigint not null,BookName varchar(40) not null,BookTitle varchar(40) not null,primary key (BookIndex));" << endl; bool isCreated = stmt->execute(ss.str()); if (!isCreated) { cout << "Create table mt" << i << " successfully!" << endl; } ss = stringstream(); ss << "insert into mt" << i << "(BookId,BookName,BookTitle) values "; for (int j = 1; j <= 1000000; j++) { ss << "(" << ++num << ",'" << getUuid() << "','" << getUuid() << "'),"; } cout<<__LINE__<<endl; string str = ss.str().substr(0, ss.str().length() - 1); if (conn->isClosed()) { dvr = get_driver_instance(); conn = dvr->connect("tcp://127.0.0.1:3306", "root", "Password");
conn->setSchema("db"); } cout<<__LINE__<<endl; isCreated = stmt->execute(str); cout<<__LINE__<<endl; if (!isCreated) { cout << getTime() << ",num=" << num << ",Insert into mt" << i << " successfully!" << endl; } ss = stringstream(); cout<<"IsClosed="<<conn->isClosed()<<endl; } delete stmt; delete pstmt; delete conn; } catch (sql::SQLException &ex) { cout << "#ERR: SQLException in " << __FILE__ << "," << __FUNCTION__ << "," << __LINE__ << endl; cout << ex.what() << endl; cout << "Error code:" << ex.getErrorCode() << ",state:" << ex.getSQLState() << endl; return; } cout << getTime() << ",finished in " << __FUNCTION__ << endl; } char *Util::getTime() { time_t rawTime = time(NULL); struct tm tmInfo = *localtime(&rawTime); strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo); return dtValue; } char *Util::getUuid() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID, uuidValue); return uuidValue; }

5.main.cpp

#include "Model/Util.h"

void mysql2(int len);

int main(int args,char **argv)
{
    try
    {
        mysql2(atoi(argv[1]));
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << 'n';
    }

}

void mysql2(int len)
{
    Util ul;
    ul.mysql2(len);
}

6.Compile via g++ in below command

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -lmysqlcppconn

 

7.Run the compiled program via 

time ./h1 10000;

Linux C++  mysql  create table and insert data into table,mysqlconnector set global net_read_timeout=3600

 

 

8.use db;show tables;

Linux C++  mysql  create table and insert data into table,mysqlconnector set global net_read_timeout=3600

  

原文链接: https://www.cnblogs.com/Fred1987/p/16375832.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Linux C++  mysql  create table and insert data into table,mysqlconnector set global net_read_timeout=3600

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401405

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:14
下一篇 2023年4月19日 上午9:14

相关推荐