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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include "SendMsg2Mysql.h" #include <string.h> #include <iostream> #include <vector> #include <fstream> #include <sstream>
std::vector<std::vector<std::string>> read_csv_chunk(const std::string& filename, size_t chunk_size, size_t start_line) { std::ifstream file(filename); std::vector<std::vector<std::string>> data; std::string line; size_t current_line = 0;
while (current_line < start_line && std::getline(file, line)) { current_line++; }
while (std::getline(file, line) && data.size() < chunk_size) { std::istringstream iss(line); std::vector<std::string> tokens; std::string token; while (std::getline(iss, token, ',')) { tokens.push_back(token); } data.push_back(tokens); current_line++; } return data; }
int SendMsg2Mysql::init(std::string host, std::string user, std::string passwd, std::string db, unsigned int port) { mysql_init(&mysql); if(mysql_real_connect(&mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, NULL, 0) == nullptr) { fprintf(stderr, "%s\n", mysql_error(&mysql)); std::cerr << "Failed to connect to MySQL user!\n"; return -1; } std::cout << "Successfully to connect to MySQL user!\n"; mysql_set_character_set(&mysql, "utf8"); return 0; }
int SendMsg2Mysql::insertBig(std::string path, std::string table, bool ignore1lines) { std::string a = "LOAD DATA LOCAL INFILE '"; std::string b = "' INTO TABLE "; std::string c = " FIELDS TERMINATED BY ','"; std::string d = " IGNORE 1 LINES "; std::string insert_query = ignore1lines ? (a + path + b + table + c + d) : (a + path + b + table + c); if (mysql_query(&mysql, insert_query.c_str())) { std::cerr << "Error: " << mysql_error(&mysql) << std::endl; mysql_close(&mysql); return -1; } }
int SendMsg2Mysql::insert(std::string path, std::string table, int chunk_size, bool ignore1lines) { if(ignore1lines) start_line=1; while (true) { std::vector<std::vector<std::string>> csv_data = read_csv_chunk(path, chunk_size, start_line); if (csv_data.empty()) { break; }
for (const auto& row : csv_data) { std::string a = "INSERT INTO "; std::string b = " VALUES ("; std::string insert_query = a + table + b; for (size_t i = 0; i < row.size(); ++i) { insert_query += "'" + row[i] + "'"; if (i < row.size() - 1) { insert_query += ", "; } } insert_query = insert_query + ")"; if (mysql_query(&mysql, insert_query.c_str())) { std::cerr << "Error: " << mysql_error(&mysql) << std::endl; mysql_close(&mysql); return -1; } }
start_line += chunk_size; } }
int SendMsg2Mysql::insert(std::string table, std::vector<float>& data) { MYSQL_STMT *stmt = mysql_stmt_init(&mysql); MYSQL_BIND *bind = new MYSQL_BIND[data.size()]; if (!stmt) { fprintf(stderr, "初始化语句失败: %s\n", mysql_error(&mysql)); return -1; } std::string a = "INSERT INTO "; std::string c = " VALUES(?"; for(int i=1; i<data.size(); ++i) { c = c + ",?"; } c = c + ")"; std::cout<<"HI"<<std::endl; std::string query = a + table + c; if (mysql_stmt_prepare(stmt, query.c_str(), query.length())) { fprintf(stderr, "准备语句失败: %s\n", mysql_stmt_error(stmt)); return -1; }
unsigned long length = data.size() * sizeof(float); memset(bind, 0, sizeof(bind)); for (int i = 0; i < data.size(); ++i) { bind[i].buffer_type = MYSQL_TYPE_FLOAT; bind[i].buffer = &data[i]; bind[i].is_null = 0; } if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, "绑定参数失败: %s\n", mysql_stmt_error(stmt)); return -1; }
if (mysql_stmt_execute(stmt)) { fprintf(stderr, "执行语句失败: %s\n", mysql_stmt_error(stmt)); return -1; }
mysql_stmt_close(stmt); }
void SendMsg2Mysql::close() { mysql_close(&mysql); }
|