首页 > 数据库 >mysql c++ create table,insert,select

mysql c++ create table,insert,select

时间:2023-11-21 22:56:15浏览次数:52  
标签:std insert include varchar DEFAULT create c++ time NULL

CREATE TABLE `t1` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT primary key,
  `author` varchar(40) NOT NULL DEFAULT '',
  `comment` varchar(40) NOT NULL DEFAULT '',
  `content` varchar(40) NOT NULL DEFAULT '',
  `header` varchar(40) NOT NULL DEFAULT '',
  `isbn` varchar(40) NOT NULL DEFAULT '',
  `memory` varchar(40) NOT NULL DEFAULT '',
  `object` varchar(40) NOT NULL DEFAULT '',
  `result` varchar(40) NOT NULL DEFAULT '',
  `summary` varchar(40) NOT NULL DEFAULT '',
  `title` varchar(40) NOT NULL DEFAULT '',
  `topic` varchar(40) NOT NULL DEFAULT '' 
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <pqxx/pqxx>
#include <random>
#include <set>
#include <thread>
#include <uuid/uuid.h>
#include <vector>
#include <cppconn/driver.h>
#include <cppconn/metadata.h>
#include <cppconn/parameter_metadata.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/resultset_metadata.h>
#include <cppconn/statement.h> 

template <typename T1, typename T2>
void print_mtx_map(const std::map<T1, T2> _mp, const int &interval);

std::string get_time_now(bool is_exact = true)
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    if (is_exact)
    {
        std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
        std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
        std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
        std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
        ss << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
           << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
           << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    }
    return ss.str();
}

std::string get_time_span(std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, std::chrono::time_point<std::chrono::high_resolution_clock> _end_time)
{
    std::stringstream ss;
    ss << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
       << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
       << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
       << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos"
       << std::endl;
    return ss.str();
}

char uuid_value[37];
uint32_t rand32()
{
    return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff);
}

char *gen_uuid4()
{
    int n = snprintf(uuid_value, sizeof(uuid_value), "%08x-%04x-%04x-%04x-%04x%08x",
                     rand32(),                       // Generates a 32-bit Hex number
                     rand32() & 0xffff,              // Generates a 16-bit Hex number
                     ((rand32() & 0x0fff) | 0x4000), // Generates a 16-bit Hex number of the form 4xxx (4 indicates the UUID version)
                     (rand32() & 0x3fff) + 0x8000,   // Generates a 16-bit Hex number in the range [0x8000, 0xbfff]
                     rand32() & 0xffff, rand32());   // Generates a 48-bit Hex number
    // return n >= 0 && n < len;             // Success only when snprintf result is a positive number and the provided buffer was large enough.
    return uuid_value;
}
 
void insert_into_mysql(const int &loops)
{
    /*CREATE TABLE `t1` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT primary key,
  `author` varchar(40) NOT NULL DEFAULT '',
  `comment` varchar(40) NOT NULL DEFAULT '',
  `content` varchar(40) NOT NULL DEFAULT '',
  `header` varchar(40) NOT NULL DEFAULT '',
  `isbn` varchar(40) NOT NULL DEFAULT '',
  `memory` varchar(40) NOT NULL DEFAULT '',
  `object` varchar(40) NOT NULL DEFAULT '',
  `result` varchar(40) NOT NULL DEFAULT '',
  `summary` varchar(40) NOT NULL DEFAULT '',
  `title` varchar(40) NOT NULL DEFAULT '',
  `topic` varchar(40) NOT NULL DEFAULT '' 
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;*/
    sql::Driver *driver = get_driver_instance();
    sql::Connection *conn = driver->connect("localhost", "sam01", "Sam0001!");
    sql::ResultSet *res;
    sql::Statement *stmt = conn->createStatement();
    conn->setSchema("db");
    std::stringstream ss;
    srand(time(NULL));
    std::string sql_str;
    int last_comma_idx = -1;
    bool is_inserted = false;
    std::uint64_t num = 0;
    std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
    for (int i = 0; i < loops; i++)
    {
        ss = std::stringstream();
        _start_time = std::chrono::high_resolution_clock::now();
        ss << "insert into t1(author,comment,content,header,isbn,memory,object,result,summary,title,topic) values ";
        for (int j = 0; j < 1000000; j++)
        {
            ss
                << "('" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4()
                << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4()
                << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "'),";
            ++num;
        }
        sql_str = ss.str();
        last_comma_idx = sql_str.find_last_of(",");
        sql_str = sql_str.substr(0, last_comma_idx);
        is_inserted = stmt->execute(sql_str);
        _end_time = std::chrono::high_resolution_clock::now();
        std::cout << std::boolalpha << get_time_now() << "," << is_inserted << ",loops:" << i + 1 << ",num:" << num << ",time cost:"
                  << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                  << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                  << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                  << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!" << std::endl;
    }
    conn->close();
    std::cout << std::boolalpha << get_time_now() << ",num:" << num << ",finished line:" << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

void select_from_mysql()
{
    sql::Driver *driver = get_driver_instance();
    sql::Connection *conn = driver->connect("localhost", "sam01", "Sam0001!");
    conn->setSchema("db");
    sql::Statement *stmt = conn->createStatement();
    std::string select_str = "select * from t1;";
    sql::ResultSet *res = stmt->executeQuery(select_str);
    sql::ResultSetMetaData *resMetadata = res->getMetaData();
    int cols_count = resMetadata->getColumnCount();
    int rows_count = res->rowsCount();
    std::cout << "Rows count:" << rows_count << ",columns count:" << cols_count << std::endl;
    std::uint64_t rows_idx = 0;
    while (res->next())
    {
        if (++rows_idx % 1000000 == 0)
        {
            for (int j = 1; j < cols_count - 1; j++)
            {
                std::cout << res->getString(j) << ",";
            }
            std::cout << res->getString(cols_count - 1) << std::endl;
        }
    }
    std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

int main(int args, char **argv)
{ 
    // g++-13 -std=c++23 -I. main.cpp -lmysqlcppconn -o h1;    
    insert_into_mysql(atoi(argv[1]));
    // select_from_mysql();
    std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

 

//Compile
g++-13 -std=c++23 -I. main.cpp -lmysqlcppconn -o h1;

 

//run
nohup ./h1 100 >> insertmysql.txt |tail -f insertmysql.txt;

 

int main(int args, char **argv)
{ 
    // g++-13 -std=c++23 -I. main.cpp -lmysqlcppconn -o h1;    
    // insert_into_mysql(atoi(argv[1]));
    select_from_mysql();
    std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

//compile
g++-13 -std=c++23 -I. main.cpp -lmysqlcppconn -o h1; 

//run
nohup ./h1 >>select.txt |tail -f select.txt;

 

 

//insert snippet
        ss = std::stringstream();
        _start_time = std::chrono::high_resolution_clock::now();
        ss << "insert into t1(author,comment,content,header,isbn,memory,object,result,summary,title,topic) values ";
        for (int j = 0; j < 1000000; j++)
        {
            ss
                << "('" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4()
                << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4()
                << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "'),";
            ++num;
        }
        sql_str = ss.str();
        last_comma_idx = sql_str.find_last_of(",");
        sql_str = sql_str.substr(0, last_comma_idx);
        is_inserted = stmt->execute(sql_str);

 

 

 

//select snippet
    sql::Statement *stmt = conn->createStatement();
    std::string select_str = "select * from t1;";
    sql::ResultSet *res = stmt->executeQuery(select_str);
    sql::ResultSetMetaData *resMetadata = res->getMetaData();
    int cols_count = resMetadata->getColumnCount();
    int rows_count = res->rowsCount();
    std::cout << "Rows count:" << rows_count << ",columns count:" << cols_count << std::endl;
    std::uint64_t rows_idx = 0;
    while (res->next())
    {
        if (++rows_idx % 1000000 == 0)
        {
            for (int j = 1; j < cols_count - 1; j++)
            {
                std::cout << res->getString(j) << ",";
            }
            std::cout << res->getString(cols_count - 1) << std::endl;
        }
    }

 

标签:std,insert,include,varchar,DEFAULT,create,c++,time,NULL
From: https://www.cnblogs.com/Fred1987/p/17847826.html

相关文章

  • C++ LibCurl实现Web指纹识别
    Web指纹识别是一种通过分析Web应用程序的特征和元数据,以确定应用程序所使用的技术栈和配置的技术。这项技术旨在识别Web服务器、Web应用框架、后端数据库、JavaScript库等组件的版本和配置信息。通过分析HTTP响应头、HTML源代码、JavaScript代码、CSS文件等,可以获取关于Web应用程......
  • VC++ 2019 MFC TinyXML2使用教程/方法详解(转载)
    转载地址:VC++2019MFCTinyXML2使用教程/方法详解_vc++2019_一笑的博客-CSDN博客TinyXML2让VC++中操作XML,如鱼得水,就像一个小型的数据库,特别方便。本篇主要介绍在VC++2019的MFC项目中,如何利用TinyXML2,创建、插入、查询、更新、删除节点或数据。也顺便介绍下UNICODE转UTF......
  • CreatePartition API执行流程_milvus源码解析(2)
    CreatePartitionAPI执行流程源码解析milvus版本:v2.3.2syncNewCreatedPartitionStep_milvus源码解析整体架构:CreatePartition的数据流向:1.客户端sdk发出CreatePartitionAPI请求。frompymilvusimport(connections,Collection,Partition,)print("star......
  • c++文件的操作
    文件操作:c++对文件的操作需要包含头文件<fstream>文件的类型,主要分为文本文件(ASCII形式存在电脑) 和二进制文件。文件操作方式:1.写文件(ofstream)2.读文件(ifstream)3.读写文件(fstream) 写文件步骤:1.包含头文件》2.创建流对象》3.打开文件》4.写数据》5.关闭流#inc......
  • c++总结
    const在不同位置时的不同意义指针类型前:声明一个指向常量的指针,程序中不能通过指针来改变它所指向的值,但指针本身的值可以改变,即指针可以指向其他数据;"*"号和指针名之间,声明一个指针常量(常指针),指针本身的值不可改变,即不能指向其他数据,但指向的数据的值可以改变两个地方都加,声明......
  • 医院影像归档和通信系统源码(C++pacs系统源码)
    PACS系统,意为影像归档和通信系统。它是应用在医院影像科室的系统,主要的任务就是把日常产生的各种医学影像(包括核磁,CT,超声,各种X光机,各种红外仪、显微仪等设备产生的图像)通过各种接口(模拟,DICOM,网络)以数字化的方式海量保存起来,当需要的时候在一定的授权下能够很快的调回使用,同时增加一......
  • C++ LibCurl实现Web隐藏目录扫描
    LibCurl是一个开源的免费的多协议数据传输开源库,该框架具备跨平台性,开源免费,并提供了包括HTTP、FTP、SMTP、POP3等协议的功能,使用libcurl可以方便地进行网络数据传输操作,如发送HTTP请求、下载文件、发送电子邮件等。它被广泛应用于各种网络应用开发中,特别是涉及到数据传输的场景。......
  • C++ 20 编译期类型名获取
    编译期类型名获取C++20标准,使用库std::source_location。#include<source_location>C++20之前在C++20前有两种方法__PRETTY_FUNCTION____FUNCSIG__通过截取函数签名中的T=...获取函数类型。template<typenameT>constexprautotype_name()->std::stri......
  • 【尝试逆向】零基础尝试寻找某个C++游戏的文件读取方法
    前言本游戏在国内知名度非常一般,而且在游戏领域也算是非常少见的厂商完全不考虑国际化的游戏系列,距今已有近30年的历史。这次为了尝试对此游戏的贴图进行提取,我尝试下载了本游戏系列的大概所有版本,并尝试通过脱壳等手段找到贴图的提取函数,并想办法写出来提取用的脚本。不过目前......
  • 【C++】【OpenCV】【NumPy】图像数据的访问
    接上一随笔,这次学习针对图像数据的访问(Numpy.array)在OpenCV中,使用imread()方法可以访问图像,其返回值是一个数组,而根据传入的不同图像,将会返回不同维度的数组。针对返回的图像数据,即数组,我们是可以进行操作的:1importcv223#MyPic.png图像自行随意创建一个原始字符转换......