cppconn cpp prepared statement insert into table

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

 

cppconn cpp prepared statement insert into table

 

 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

 

 

 

cppconn cpp prepared statement insert into table

 

 cppconn cpp prepared statement insert into table

 

 

Truthfully speaking,due to lack of bulk insert at once and execute insert one by one,so the performance is not idea.

 

原文链接: https://www.cnblogs.com/Fred1987/p/17013826.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    cppconn cpp prepared statement insert into table

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401182

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:09
下一篇 2023年4月19日 上午9:09

相关推荐