• 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;

      

  • 相关阅读:
    修炼一名程序员的职业水准_转
    system执行shell命令
    (转)MySql数据库索引原理(总结性)
    (转)SQL Server基础之存储过程(清晰使用)
    (转)菜鸟学数据库(三)——存储过程
    (转)菜鸟学数据库(二)——触发器
    (转)SqlServer基础之(触发器)(清晰易懂)
    (转)菜鸟学数据库(一)——三范式
    (转)HTTP 协议详解(基础)
    (转)HTTP协议漫谈
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16375832.html
Copyright © 2020-2023  润新知