linux cc++ 实现SELECT SRC_IP SRC_MAC DST_IP DST_PORT PROTO FROM test; 1亿多条数据 分页查询 数据库是mysql。给出实现代码
以下是一个示例的C++代码,用于在Linux上使用MySQL C++ Connector实现分页查询1亿多条数据的SELECT语句。
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <iostream>
using namespace std;
using namespace sql;
int main() {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); // 替换为实际的MySQL连接信息
con->setSchema("database_name"); // 替换为实际的数据库名称
int perPage = 1000; // 每页查询的记录数
int currentPage = 1; // 当前页码
while (true) {
sql::Statement *stmt;
sql::ResultSet *res;
stmt = con->createStatement();
string query = "SELECT SRC_IP, SRC_MAC, DST_IP, DST_PORT, PROTO FROM test LIMIT " + to_string((currentPage - 1) * perPage) + ", " + to_string(perPage);
res = stmt->executeQuery(query);
while (res->next()) {
// 读取每一行数据的字段值
string srcIP = res->getString("SRC_IP");
string srcMAC = res->getString("SRC_MAC");
string dstIP = res->getString("DST_IP");
int dstPort = res->getInt("DST_PORT");
string proto = res->getString("PROTO");
// 处理数据
// ...
// 输出数据
cout << "SRC_IP: " << srcIP << ", SRC_MAC: " << srcMAC << ", DST_IP: " << dstIP << ", DST_PORT: " << dstPort << ", PROTO: " << proto << endl;
}
delete res;
delete stmt;
// 判断是否还有下一页数据
if (res->rowsCount() < perPage) {
break;
}
currentPage++;
}
delete con;
return 0;
}
请注意替换代码中的以下部分:
tcp://127.0.0.1:3306:替换为实际的MySQL连接信息,例如数据库的IP地址和端口号。"username"和"password":替换为实际的MySQL用户名和密码。"database_name":替换为实际的数据库名称。"SELECT SRC_IP, SRC_MAC, DST_IP, DST_PORT, PROTO FROM test":替换为实际的SELECT语句和表名。
此示例使用MySQL C++ Connector库来连接MySQL数据库,并使用LIMIT子句进行分页查询。每次查询的记录数由perPage变量指定,当前页码由currentPage变量指定。在循环中,通过逐行读取结果集中的数据,并根据需要进行处理和输出。循环将继续,直到没有更多的数据可供查询
原文地址: https://www.cveoy.top/t/topic/hZyt 著作权归作者所有。请勿转载和采集!