C++ cppconn insert into mysql tables

#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>

using namespace std;

static char *uuidValue = (char *)malloc(40);

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


void insertMysql66(int loops);

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

void insertMysql66(int loops)
{
    try
    {
        sql::Driver *driver;
        sql::Connection *conn;
        sql::PreparedStatement *prep_stmt;

        Util ul;
        stringstream ss;
        driver = get_driver_instance();
        conn = driver->connect("tcp://127.0.0.1:3306", "root", "Password");
        conn->setSchema("myDB");

        prep_stmt = conn->prepareStatement("insert into mt(BookId,BookName,BookTitle) values(?,?,?)");
        for (int i = 0; i < loops; i++)
        {
            prep_stmt->setInt64(1, i * i);
            prep_stmt->setString(2, ul.getUuid());
            prep_stmt->setString(3, ul.getUuid());
            bool isOk = prep_stmt->execute();
            if (isOk)
            {
                cout << "Failed index=" << i << endl;
            }
        }
        delete prep_stmt;
        delete conn;
    }
    catch (sql::SQLException &e)
    {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
}

Compile:

g++ -g -std=c++2a -I. -Wall -I/usr/include/cppconn *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -L/usr/lib -lmysqlcppconn

execute:

time ./h1 10000

 

C++ cppconn insert into mysql tables

 

 

Then query the result via select * from mt and below snapshot has illustrated that inserts into table successfully!

C++ cppconn insert into mysql tables

 

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

欢迎关注

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

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

    C++ cppconn insert into mysql tables

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

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

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

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

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

相关推荐