2019-07-28 10:31:56 +08:00
|
|
|
|
// sqlite.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
|
2014-11-19 00:25:21 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2016-12-03 20:09:21 +08:00
|
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
const char* CREATE_TBL =
|
|
|
|
|
"create table group_tbl\r\n"
|
|
|
|
|
"(\r\n"
|
|
|
|
|
"group_name varchar(128) not null,\r\n"
|
|
|
|
|
"uvip_tbl varchar(32) not null default 'uvip_tbl',\r\n"
|
|
|
|
|
"access_tbl varchar(32) not null default 'access_tbl',\r\n"
|
|
|
|
|
"access_week_tbl varchar(32) not null default 'access_week_tbl',\r\n"
|
|
|
|
|
"access_month_tbl varchar(32) not null default 'access_month_tbl',\r\n"
|
|
|
|
|
"update_date date not null default '1970-1-1',\r\n"
|
|
|
|
|
"disable integer not null default 0,\r\n"
|
|
|
|
|
"add_by_hand integer not null default 0,\r\n"
|
|
|
|
|
"class_level integer not null default 0,\r\n"
|
|
|
|
|
"primary key(group_name, class_level)\r\n"
|
|
|
|
|
")";
|
|
|
|
|
|
|
|
|
|
static bool tbl_create(acl::db_handle& db)
|
|
|
|
|
{
|
|
|
|
|
if (db.tbl_exists("group_tbl"))
|
|
|
|
|
{
|
|
|
|
|
printf("table exist\r\n");
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
else if (db.sql_update(CREATE_TBL) == false)
|
|
|
|
|
{
|
|
|
|
|
printf("sql error\r\n");
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("create table ok\r\n");
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static bool tbl_insert(acl::db_handle& db, int n)
|
|
|
|
|
{
|
|
|
|
|
const char* sql_fmt = "insert into group_tbl('group_name', 'uvip_tbl')"
|
2019-07-28 10:31:56 +08:00
|
|
|
|
" values('<27>й<EFBFBD><D0B9><EFBFBD>-%d', 'test')";
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string sql;
|
|
|
|
|
sql.format(sql_fmt, n);
|
|
|
|
|
if (db.sql_update(sql.c_str()) == false)
|
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
|
|
const acl::db_rows* result = db.get_result();
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
const std::vector<acl::db_row*>& rows = result->get_rows();
|
|
|
|
|
for (size_t i = 0; i < rows.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
const acl::db_row* row = rows[i];
|
|
|
|
|
for (size_t j = 0; j < row->length(); j++)
|
|
|
|
|
printf("%s, ", (*row)[j]);
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.free_result();
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static int tbl_select(acl::db_handle& db, int n)
|
|
|
|
|
{
|
|
|
|
|
const char* sql_fmt = "select * from group_tbl where"
|
2019-07-28 10:31:56 +08:00
|
|
|
|
" group_name='<27>й<EFBFBD><D0B9><EFBFBD>-%d' and uvip_tbl='test'";
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string sql;
|
|
|
|
|
sql.format(sql_fmt, n);
|
|
|
|
|
|
|
|
|
|
if (db.sql_select(sql.c_str()) == false)
|
|
|
|
|
{
|
|
|
|
|
printf("select sql error\r\n");
|
|
|
|
|
return (-1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>г<EFBFBD><D0B3><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
const acl::db_rows* result = db.get_result();
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
const std::vector<acl::db_row*>& rows = result->get_rows();
|
|
|
|
|
for (size_t i = 0; i < rows.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (n >= 5)
|
|
|
|
|
continue;
|
|
|
|
|
const acl::db_row* row = rows[i];
|
|
|
|
|
for (size_t j = 0; j < row->length(); j++)
|
|
|
|
|
printf("%s, ", (*row)[j]);
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>г<EFBFBD><D0B3><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
for (size_t i = 0; i < db.length(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (n >= 5)
|
|
|
|
|
continue;
|
|
|
|
|
const acl::db_row* row = db[i];
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>¼<EFBFBD><C2BC>ij<EFBFBD><C4B3><EFBFBD>ֶε<D6B6>ֵ
|
2014-11-19 00:25:21 +08:00
|
|
|
|
const char* ptr = (*row)["group_name"];
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf(("error, no group name\r\n"));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
printf("group_name=%s: ", ptr);
|
|
|
|
|
for (size_t j = 0; j < row->length(); j++)
|
|
|
|
|
printf("%s, ", (*row)[j]);
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ret = (int) db.length();
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
db.free_result();
|
|
|
|
|
return (ret);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static bool tbl_delete(acl::db_handle& db, int n)
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
const char* sql_fmt = "delete from group_tbl where group_name='<27>й<EFBFBD><D0B9><EFBFBD>-%d'";
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
acl::string sql;
|
|
|
|
|
sql.format(sql_fmt, n);
|
|
|
|
|
|
|
|
|
|
if (db.sql_update(sql.c_str()) == false)
|
|
|
|
|
{
|
|
|
|
|
printf("delete sql error\r\n");
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < db.length(); i++)
|
|
|
|
|
{
|
|
|
|
|
const acl::db_row* row = db[i];
|
|
|
|
|
for (size_t j = 0; j < row->length(); j++)
|
|
|
|
|
printf("%s, ", (*row)[j]);
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
}
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
db.free_result();
|
|
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 22:35:31 +08:00
|
|
|
|
class db_thread : public acl::thread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
db_thread(acl::db_sqlite& db, acl::locker& lk, int min, int max)
|
|
|
|
|
: db_(db), lk_(lk), min_(min), max_(max) {}
|
|
|
|
|
~db_thread(void) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void* run(void)
|
|
|
|
|
{
|
|
|
|
|
for (int i = min_; i < max_; i++)
|
|
|
|
|
{
|
|
|
|
|
lk_.lock();
|
|
|
|
|
if (tbl_insert(db_, i))
|
|
|
|
|
printf(">>insert ok: i=%d, affected: %d\r",
|
|
|
|
|
i, db_.affect_count());
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf(">>insert error: i = %d\r\n", i);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
lk_.unlock();
|
|
|
|
|
}
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
printf(">>insert total affect: %d\n", db_.affect_total_count());
|
|
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
for (int i = min_; i < max_; i++)
|
|
|
|
|
{
|
|
|
|
|
lk_.lock();
|
|
|
|
|
int ret = tbl_select(db_, i);
|
|
|
|
|
lk_.unlock();
|
|
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
{
|
|
|
|
|
n += ret;
|
|
|
|
|
printf(">>select ok: i=%d, ret=%d\r", i, ret);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf(">>select error: i = %d\r\n", i);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
printf(">>select total: %d\r\n", n);
|
|
|
|
|
|
|
|
|
|
for (int i = min_; i < max_; i++)
|
|
|
|
|
{
|
|
|
|
|
lk_.lock();
|
|
|
|
|
if (tbl_delete(db_, i))
|
|
|
|
|
printf(">>delete ok: %d, affected: %d\r",
|
|
|
|
|
i, (int) db_.affect_count());
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf(">>delete error: i = %d\r\n", i);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
lk_.unlock();
|
|
|
|
|
}
|
|
|
|
|
printf("\r\n");
|
|
|
|
|
printf(">>delete total affected: %d\n", db_.affect_total_count());
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
acl::db_sqlite& db_;
|
|
|
|
|
acl::locker& lk_;
|
|
|
|
|
int min_;
|
|
|
|
|
int max_;
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
int main(void)
|
|
|
|
|
{
|
2016-12-03 20:09:21 +08:00
|
|
|
|
acl::acl_cpp_init();
|
2017-02-21 22:35:31 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
2016-12-03 20:09:21 +08:00
|
|
|
|
|
|
|
|
|
acl::stdin_stream in;
|
|
|
|
|
acl::stdout_stream out;
|
|
|
|
|
acl::string line;
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
const char* libname = "sqlite3.dll";
|
|
|
|
|
#else
|
|
|
|
|
const char* libname = "libsqlite3.so";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
acl::string path;
|
|
|
|
|
out.format("Enter %s load path: ", libname);
|
|
|
|
|
if (in.gets(line) && !line.empty())
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
path.format("%s\\%s", line.c_str(), libname);
|
|
|
|
|
#else
|
|
|
|
|
path.format("%s/%s", line.c_str(), libname);
|
|
|
|
|
#endif
|
|
|
|
|
else
|
|
|
|
|
path = libname;
|
|
|
|
|
|
|
|
|
|
out.format("%s path: %s\r\n", libname, path.c_str());
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ȫ·<C8AB><C2B7>
|
2016-12-03 20:09:21 +08:00
|
|
|
|
acl::db_handle::set_loadpath(path);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
//acl::string dbfile("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>.db");
|
2018-05-31 17:10:27 +08:00
|
|
|
|
acl::string dbfile("./path1/path2/mydb.db");
|
2016-12-03 20:09:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// db_sqlite <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> set_loadpath ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA> db_sqlite <20><>
|
|
|
|
|
// <20><><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>м<EFBFBD><D0BC><EFBFBD> libsqlite3.so
|
2017-02-21 22:35:31 +08:00
|
|
|
|
acl::db_sqlite db(dbfile, "gbk");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
if (db.open() == false)
|
|
|
|
|
{
|
|
|
|
|
printf("open dbfile: %s error\r\n", dbfile.c_str());
|
|
|
|
|
getchar();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("open dbfile %s ok\r\n", dbfile.c_str());
|
|
|
|
|
|
|
|
|
|
if (tbl_create(db) == false)
|
|
|
|
|
{
|
|
|
|
|
printf("create table error\r\n");
|
|
|
|
|
getchar();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
db.set_conf("PRAGMA synchronous = off");
|
|
|
|
|
db.set_conf("PRAGMA encoding = \"UTF-8\"");
|
|
|
|
|
acl::string buf;
|
|
|
|
|
if ((db.get_conf("PRAGMA encoding", buf)))
|
|
|
|
|
printf(">>PRAGMA encoding: %s\r\n", buf.c_str());
|
|
|
|
|
db.show_conf();
|
|
|
|
|
|
2017-02-21 22:35:31 +08:00
|
|
|
|
int max = 10000, nstep = 100;
|
|
|
|
|
std::vector<db_thread*> threads;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2017-02-21 22:35:31 +08:00
|
|
|
|
acl::meter_time(__FILE__, __LINE__, "---begin---");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2017-02-21 22:35:31 +08:00
|
|
|
|
acl::locker lk;
|
|
|
|
|
int j;
|
|
|
|
|
for (int i = 0; i < max; i = j)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2017-02-21 22:35:31 +08:00
|
|
|
|
j = i + nstep;
|
|
|
|
|
db_thread* thread = new db_thread(db, lk, i, j);
|
|
|
|
|
threads.push_back(thread);
|
|
|
|
|
thread->set_detachable(false);
|
|
|
|
|
thread->start();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 22:35:31 +08:00
|
|
|
|
for (std::vector<db_thread*>::iterator it = threads.begin();
|
|
|
|
|
it != threads.end(); ++it)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2017-02-21 22:35:31 +08:00
|
|
|
|
(*it)->wait();
|
|
|
|
|
delete *it;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
2017-02-21 22:35:31 +08:00
|
|
|
|
|
|
|
|
|
acl::meter_time(__FILE__, __LINE__, "---end---");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
printf("Enter any key to exit.\r\n");
|
|
|
|
|
getchar();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|