diff --git a/txt2sqlite/main.cpp b/txt2sqlite/main.cpp new file mode 100644 index 0000000..38fdeef --- /dev/null +++ b/txt2sqlite/main.cpp @@ -0,0 +1,134 @@ +// +// main.cpp +// txt2sqlite +// +// Created by 虚幻 on 2020/10/12. +// Copyright © 2020 qwertyyb. All rights reserved. +// + +#include +#include +#include +#include +#include +#include + +using namespace std; + +vector split(const string& s, const char delim = ' ') { + vector sv; + istringstream iss(s); + string temp; + + while (getline(iss, temp, delim)) { + sv.emplace_back(move(temp)); + } + + return sv; +} + +string get_sql(vector columns) { + vector rows; + + string code = columns[0]; + + for (int i = 1; i < columns.size(); i++) { + vector row; + row.push_back("'" + code + "'"); + row.push_back("'" + columns[i] + "'"); + string rowstr = "('" + code + "', '" + columns[i] + "')"; + rows.push_back(rowstr); + } + + stringstream res; + copy(rows.begin(), rows.end(), ostream_iterator(res, ", ")); + return res.str(); +} + +void create_table(sqlite3 *db, string tableName = "dict_wb") { + string sql = "create table if not exists " + tableName + "(" \ + "id integer primary key autoincrement not null, " \ + "code char(10) not null," \ + "text text not null" \ + ")"; + int rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL); + if (rc == SQLITE_OK) { + cout<<"dict table created successfully"<> dict; + + string line; + while(getline(infile, line)) { + dict.emplace_back(split(line)); + } + + + vector rowstrs; + + for (auto line : dict) { + rowstrs.push_back(get_sql(line)); + } + + + + sqlite3 *db; + + int err = sqlite3_open("/Users/marchyang/db.sqlite3", &db); + + if (err){ + cerr << "Can't open database: " << sqlite3_errmsg(db) << endl; + exit(0); + } + + create_table(db, "wb_table"); + + cout << "count:" << rowstrs.size() << endl; + + // 分批插入 + int step = 100000; //每次插入数量 + vector::iterator begin = rowstrs.begin(); + for(int i = 0; i < rowstrs.size(); i+= step) { + vector::iterator start = begin + i; + unsigned long length = rowstrs.size() - i > step ? step : rowstrs.size() - i; + + vector::iterator end = start + length; + + stringstream res; + copy(start, end, ostream_iterator(res, " ")); + + string sql = "insert into wb_table(code, text) values" + res.str(); + sql.pop_back(); + sql.pop_back(); + sql.pop_back(); + + + int rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL); + if (rc == SQLITE_OK) { + cout<<"insert successfully"<