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

  • 相关阅读:
    报表中的Excel操作之Aspose.Cells(Excel模板)
    .NET开源组件
    JSON 和 JSONP
    servlet 中getLastModified()
    spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。
    spring mvc dispatcherservlet处理request流程
    log显示error时的堆栈信息理解和分析
    web项目log日志查看分析->流程理解
    war包结构
    Spring Boot干货系列:(三)启动原理解析
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16439653.html
Copyright © 2020-2023  润新知