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;

 

 

8.use db;show tables;

  

posted @   FredGrit  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示