//model/util.h #pragma once #ifndef __util_h__ #define __util_h__ #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <string> #include <string.h> #include <unistd.h> #include <uuid/uuid.h> #include <vector> #include <jsoncpp/json/json.h> #include <cppconn/connection.h> #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/prepared_statement.h> #include <cppconn/resultset.h> #include <mysql/mysql.h> using namespace std; class util { public: string get_time_now(); string get_uuid(); void mysql_prep_stmt_demo(); }; #endif //model/util.cpp #include "model/util.h" std::string util::get_time_now() { chrono::time_point<chrono::high_resolution_clock> now = chrono::high_resolution_clock::now(); auto ms = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()) % 1000; time_t rawTime = chrono::high_resolution_clock::to_time_t(now); struct tm tmInfo = *localtime(&rawTime); stringstream ss; ss << std::put_time(&tmInfo, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << ms.count(); string dtStr = ss.str(); ss = stringstream(); ss.str(std::string()); return dtStr; } std::string util::get_uuid() { uuid_t newUUID; uuid_generate(newUUID); char *uuidValue = (char *)malloc(40); uuid_unparse(newUUID, uuidValue); string uuidStr(uuidValue); free(uuidValue); uuidValue = nullptr; return uuidStr; } void util::mysql_prep_stmt_demo() { sql::Driver *driver; sql::Connection *conn; sql::PreparedStatement *pstmt; try { driver=get_driver_instance(); conn=driver->connect("tcp://127.0.0.1:3306","username","password"); conn->setSchema("db"); pstmt=conn->prepareStatement("insert into b1 (id,author,comment,content,name,title,topic) values (?,?,?,?,?,?,?)"); for(int i=0;i<1000;i++) { pstmt->setUInt64(1,static_cast<uint64_t>(i)); pstmt->setString(2,get_uuid()); pstmt->setString(3,get_uuid()); pstmt->setString(4,get_uuid()); pstmt->setString(5,get_uuid()); pstmt->setString(6,get_uuid()); pstmt->setString(7,get_uuid()); pstmt->execute(); } delete pstmt; delete conn; } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } cout<<get_time_now()<<",finished in "<<__FUNCTION__<<","<<__LINE__<<endl; } //main.cpp #include "model/util.h" void mysql_prep_stmt_demo() { util ul; ul.mysql_prep_stmt_demo(); } int main(int args,char **argv) { mysql_prep_stmt_demo(); }
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -ljsoncpp -lmysqlcppconn;
Run
./h1
//mysql.sql
//show create table b1 \G Create Table: CREATE TABLE `b1` ( `idx` int NOT NULL AUTO_INCREMENT, `id` bigint NOT NULL DEFAULT '0', `author` varchar(40) NOT NULL DEFAULT '', `comment` varchar(40) NOT NULL DEFAULT '', `content` varchar(40) NOT NULL DEFAULT '', `name` varchar(40) NOT NULL DEFAULT '', `title` varchar(40) NOT NULL DEFAULT '', `topic` varchar(40) NOT NULL DEFAULT '', PRIMARY KEY (`idx`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Truthfully speaking,due to lack of bulk insert at once and execute insert one by one,so the performance is not idea.
标签:insert,NULL,uuid,cppconn,get,DEFAULT,into,include,pstmt From: https://www.cnblogs.com/Fred1987/p/17013826.html