C++ mysql read table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | //Utill.h #ifndef Util_H #define Util_H #include <chrono> #include <ctime> #include <fstream> #include <iostream> #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> #include <cppconn/prepared_statement.h> #include <sstream> #include <stdlib.h> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> using namespace std; class Util { public : static char *uuidValue; static char *dtValue; char *getTime(); char *getUuid(); void readSQL5(); }; #endif //Util.cpp #include "Model/Util.h" char *Util::dtValue = ( char *) malloc (20); char *Util::uuidValue = ( char *) malloc (40); void Util::readSQL5() { try { sql::Driver *driver; sql::Connection *conn; sql::Statement *stmt; sql::PreparedStatement *pstmt; sql::ResultSet *res; sql::PreparedStatement *tablePstmt; sql::ResultSet *tableRes; driver = get_driver_instance(); conn = driver->connect( "tcp://127.0.0.1:3306" , "root" , "password" ); conn->setSchema( "db" ); string selectTable = "select table_name from information_schema.tables where table_schema='db'" ; pstmt = conn->prepareStatement(selectTable); res = pstmt->executeQuery(); vector<string> tableVec; while (res->next()) { tableVec.push_back(res->getString(1)); } vector<string>::iterator itr = tableVec.begin(); cout << "Table:" ; while (itr != tableVec.end()) { cout << *itr << "\t" ; itr++; } cout << endl << endl; uint64_t num = 0; int loop = 0; for ( auto &tableName : tableVec) { string sql = "select * from " + tableName; tablePstmt = conn->prepareStatement(sql); tableRes = tablePstmt->executeQuery(); while (tableRes->next()) { if (++num % 500000 == 0) { cout << "Loop=" <<++loop << ",Num=" << num << ",Index=" << tableRes->getInt(1) << ",Id=" << tableRes->getInt64(2) << ",Name=" << tableRes->getString(3) << ",Title=" << tableRes->getString(4) << endl; } } cout << getTime() << ",finished in retrieving from " << tableName << endl<<endl; } conn->close(); cout << "IsClosed=" << conn->isClosed() << endl; cout << getTime() << ",finished in void Util::readSQL5()!" << endl; } catch (sql::SQLException &ex) { cout << "#ERR: SQLException in " << __FILE__ << "," << __FUNCTION__ << "," << __LINE__ << endl; cout << ex.what() << endl; cout << "Error code:" << ex.getErrorCode() << ",state:" << ex.getSQLState() << endl; return ; } } char *Util::getTime() { time_t rawTime = time (NULL); struct tm tmInfo = * localtime (&rawTime); strftime (dtValue, 20, "%Y%m%d%H%M%S" , &tmInfo); return dtValue; } char *Util::getUuid() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID, uuidValue); return uuidValue; } |
//main.cpp #include "Model/Util.h" void readTable5(); int main(int args,char **argv) { try { readTable5(); } catch(const std::exception& e) { std::cerr << e.what() << '\n'; } } void readTable5() { Util ul; ul.readSQL5(); }
Compile:
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -lmysqlcppconn;
Run
time ./h1 -1;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2020-06-15 C# StreamWriter log batch message sync and async