//Util.h #pragma once #include <chrono> #include <ctime> #include <dirent.h> #include <fstream> #include <iostream> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> using namespace std; class Util { public: static char* uuidValue; static char* dtValue; static uint64_t readNum; char* getTimeNow(); char* getUuid(); }; //Util.cpp #include "Model/Util.h" char* Util::dtValue=(char*)malloc(20); char* Util::uuidValue=(char*)malloc(40); uint64_t Util::readNum=0; char *Util::getTimeNow() { time_t rawTime=time(nullptr); 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; } //MySQLHeler.h #include <iostream> #include <mysql_connection.h> #include <mysql_driver.h> #include <mysql_error.h> #include <mysql_connection.h> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/statement.h> #include <cppconn/resultset.h> #include <cppconn/exception.h> #include <cppconn/prepared_statement.h> #include <sstream> #include "Model/Util.h" using namespace std; class MySQLHelper { public: static int num; void mySqlConnectDemo(); void tableDemo(string tableName,int loops); }; //MySQLHelper.cpp #include "Model/MySQLHeler.h" int MySQLHelper::num=0; void MySQLHelper::mySqlConnectDemo() { sql::Driver *driver; sql::Connection *conn; sql::Statement *stmt; sql::ResultSet *res; sql::ResultSetMetaData *resMeta; try { driver = get_driver_instance(); conn = driver->connect("tcp://127.0.0.1:3306", "fred", "Fred0001!"); conn->setSchema("db"); stmt = conn->createStatement(); res = stmt->executeQuery("select * from Book"); resMeta = res->getMetaData(); int fieldsCount = resMeta->getColumnCount(); cout << "ColumnsCount=" << fieldsCount << endl; while (res->next()) { cout << res->getInt(1) << "," << res->getInt64(2) << "," << res->getString(3) << "," << res->getString(4) << "," << res->getString(5) << "," << res->getString(6) << "," << res->getString(7); cout << endl << endl; } } catch (const sql::SQLException &e) { cout << "#ERROR: SQLException in " << __FILE__; cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; cout << "#ERROR:" << e.what() << " (MySQL error code: " << e.getErrorCode(); cout << ",SQLState: " << e.getSQLState() << " )" << endl; } delete res; delete stmt; delete conn; } void MySQLHelper::tableDemo(string tableName,int loops) { try { sql::Driver *driver; sql::Connection *conn; sql::Statement *stmt; sql::ResultSet *res; sql::PreparedStatement *pstmt; driver=get_driver_instance(); conn=driver->connect("tcp://127.0.0.1:3306","username,"password"); conn->setSchema("db"); stmt=conn->createStatement(); stringstream ss; ss<<"drop table if exists "<<tableName<<";"; stmt->execute(ss.str()); ss=stringstream(); ss<<"create table if not exists " << tableName << "(Idx int not null auto_increment,Id bigint not null default 0,Name varchar(40) not null default '',Title varchar(40) not null default '',Author varchar(40) not null default '',primary key (Idx));" << endl; cout<<ss.str()<<endl; if(!stmt->execute(ss.str())) { cout<<"Create table "<<tableName<<" successfully!"<<endl; } else { cout<<"Create table "<<tableName<<" failed!"<<endl; } ss=stringstream(); Util ul; for(int i=0;i<loops;i++) { ss=stringstream(); ss<<"insert into "<<tableName<<" (Id,Name,Title,Author) values "; for(int j=0;j<1000000;j++) { int64_t bigNum=(int64_t)num*num; ss<<"('"<<bigNum<<"','"<<ul.getUuid()<<"','"<<ul.getUuid()<<"','"<<ul.getUuid()<<"'),"<<endl; ++num; } string insertSQL=ss.str(); int lastIndexOfSemiColon=insertSQL.find_last_of(","); if(lastIndexOfSemiColon!=string::npos) { insertSQL=insertSQL.erase(lastIndexOfSemiColon); // cout<<"Insert SQL: "<<endl<<insertSQL<<endl; if(!stmt->execute(insertSQL)) { cout<<num<<","<<ul.getTimeNow()<<",insert successfully!"<<endl; } else { cout<<num<<","<<ul.getTimeNow()<<",insert failed!"<<endl; } } } } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } } //main.cpp #include "Model/Util.h" #include "Model/MySQLHeler.h" int main(int args, char **argv) { MySQLHelper mh; mh.tableDemo(argv[1],atoi(argv[2])); }
Compile:
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -ljsoncpp -luuid -lmysqlcppconn
Run
./h1 tb 200
标签:Util,multiple,getString,insert,char,cpp,table,include,conn From: https://www.cnblogs.com/Fred1987/p/16950336.html