• C++ query data from mysql and limit page via key word 'limit startIndex,interval'


    //Util.h
    #ifndef Util_H
    #define Util_H
    #include <chrono>
    #include <ctime>
    #include <fstream>
    #include <functional>
    #include <future>
    #include <iostream>
    #include <random>
    #include <sstream>
    #include <thread>
    #include <unistd.h>
    #include <uuid/uuid.h>
    #include <vector>
    #include "mysql_connection.h"
    #include <cppconn/driver.h>
    #include <cppconn/resultset.h>
    #include <cppconn/exception.h>
    #include <cppconn/statement.h>
    #include <cppconn/prepared_statement.h>
    
    using namespace std;
    
    class Util
    {
    public:
        void mysqlSelectLimitPage(int interval);
    };
    
    #endif
    
    //Util.cpp
    #include "Model/Util.h"
    void Util::mysqlSelectLimitPage(int interval)
    {
        try
        {
            sql::Driver *dvr;
            sql::Connection *conn;
            sql::Statement *stmt;
            sql::ResultSet *res;
            sql::PreparedStatement *pStmt;
    
            dvr = get_driver_instance();
            conn = dvr->connect("tcp://127.0.0.1:3306", "root", "password);
            conn->setSchema("db");
    
            stmt = conn->createStatement();
            res = stmt->executeQuery("select count(*) from Book;");
            int rowsCount = -1;
            while (res->next())
            {
                rowsCount = res->getInt(1);
                cout << "rowsCount=" << rowsCount << endl;
            }
    
            int startIndex = 0;
            int loops = rowsCount / interval;
            int remainder = rowsCount % interval;
    
            stringstream sqlSS;
    
            for (int i = 0; i <= loops+1; i++)
            {
                sqlSS << "select Idx from Book limit ";
                sqlSS << startIndex << "," << interval << ";" << endl; 
                res = stmt->executeQuery(sqlSS.str());
                if (res->rowsCount() > 0)
                {
                    cout << endl << "SQL=" << sqlSS.str();
                    while (res->next())
                    {
                        cout << res->getInt(1) << "\t";
                    }
                    cout << endl << "Loop=" << i << endl;
                    startIndex += interval;
                    sqlSS = stringstream();
                }
            }
    
            delete res;
            delete stmt;
            delete conn;
        }
        catch (sql::SQLException &e)
        {
            std::cerr << e.what() << '\n';
            cout << e.getErrorCode() << endl;
            cout << e.getSQLState() << endl;
        }
    }
    //main.cpp
    #include "Model/Util.h"
    void mysqlLimitPage28(int interval);
    
    
    int main(int args, char **argv)
    {
        mysqlLimitPage28(atoi(argv[1]));
    }
    
    void mysqlLimitPage28(int interval)
    {
        Util ul;
        ul.mysqlSelectLimitPage(interval);
    }

    Compile

    g++ -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -lmysqlcppconn;

    Execute

    ./h1 100;

     

    Make  a summary,coding in for loop and the startIndex should incremente by interval,the interval is fixed.

    select Idx from Book limit startIndex,interval;

    the startIndex=startIndex+interval;

  • 相关阅读:
    DotnetCore 使用Jwks验证JwtToken签名
    HashCode
    C# RedisRateLimiter
    Centos7 使用Dockerfile 制作自己的Dotnetcore程序镜像
    ES6 HttpApplication Middleware
    请转发!简单2分钟制作无接触式小区进出微信登记表!全免费!数据安全!所有数据均存在创建人登录的QQ腾讯文档里!
    理解虚基类、虚函数与纯虚函数的概念
    不无道理
    乔布斯:不要为明天忧虑!
    【心态不好,人生易老】
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16439653.html
Copyright © 2020-2023  润新知