MySql教程

C++ mysqlclient create and insert into tables

本文主要是介绍C++ mysqlclient create and insert into tables,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#ifndef Util_H
#define Util_H
#include <functional>
#include <iostream>
#include <mysql/mysql.h>
#include <sstream>
#include <string.h>
#include <uuid/uuid.h> 
#include <vector>
#include "Model/Geeks.h"
#include "Model/Parent.h"
#include "Model/BookStruct.cpp"


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

char *Util::getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}

char *Util::getTimeNow()
{
    time_t rawTime=time(NULL);
    struct tm tmInfo=*localtime(&rawTime);
    strftime(dtValue,20,"%y%m%d%H%M%S",&tmInfo);
    return dtValue;
}

void Util::insertInBulkViaMySqlClient6(int loops)
{
    try
    {
        MYSQL *conn,mysql;
        mysql_init(&mysql);
        int queryState;

        conn=mysql_real_connect(&mysql,"localhost","root","Root0001!","db",0,0,0);
        if(conn==NULL)
        {
            cout<<mysql_error(&mysql)<<endl;
            return;
        }
        stringstream ss;
        int interval=1000000;
        string sqlStr;
        chrono::time_point<chrono::system_clock> startTime;
        chrono::time_point<chrono::system_clock> endTime;
        long double num=0;
        for(int i=1;i<=loops;i++)
        {
            startTime=chrono::system_clock::now();
            ss=stringstream();
            ss<<"create table mt"<<i<<"(BookIndex int not null auto_increment,BookId bigint not null default 0,BookName varchar(50) not null default '',BookTitle varchar(50) not null default '',primary key(BookIndex));";
            queryState=mysql_query(conn,ss.str().c_str());
            if(queryState!=0)
            {
                cout<<"Create table mt"<<i<<" failed!"<<endl;
                return;
            }            
            cout<<"Create table mt"<<i<<" successfully!"<<endl;

            ss=stringstream();
            ss<<"Insert into mt"<<i<<" (BookId,BookName,BookTitle) values ";
            for(int j=1;j<=interval;j++)
            {
                ++num;
                ss<<"("<<(i-1)*interval+j<<",'"<<getUuid()<<"','"<<getUuid()<<"'),";
            }
            sqlStr=ss.str();
            int lastCommaIndex=sqlStr.find_last_of(",");
            sqlStr=sqlStr.erase(lastCommaIndex,1);
            queryState=mysql_query(conn,sqlStr.c_str());
            if(queryState!=0)
            {
                cout<<"Insert into table mt"<<i<<" failed!"<<endl;
                return;
            }
            endTime=chrono::system_clock::now();
            cout<<"Now is "<<getTimeNow()<<",num="<<num<<",create and insert into table mt"<<i<<" successfully!,time cost:" 
            <<chrono::duration_cast<chrono::seconds>(endTime-startTime).count()<<" seconds,"
            <<chrono::duration_cast<chrono::milliseconds>(endTime-startTime).count()<<" milli,"
            <<chrono::duration_cast<chrono::microseconds>(endTime-startTime).count()<<" micro,"
            <<chrono::duration_cast<chrono::nanoseconds>(endTime-startTime).count()<<" nanos!!!"<<endl<<endl;
        }
        mysql_close(conn);
        ss=stringstream();
        cout<<"Finished in void Util::insertInBulkViaMySqlClient6(int loops) and now is "<<getTimeNow()<<endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }
    
}
void mysqlInsertInBulk4(int loops);

int main(int args,char **argv)
{
    mysqlInsertInBulk4(atoi(argv[1]));
}

void mysqlInsertInBulk4(int loops)
{
    Util ul;
    ul.insertInBulkViaMySqlClient6(loops);
}

Compile

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

Run

time ./h1 200

 

 

//Show created tables in mysql

select group_concat(table_name) from information_schema.tables where table_schema='db' and table_name regexp '[0-9]';

 

这篇关于C++ mysqlclient create and insert into tables的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!