|
|
@@ -5,9 +5,16 @@
|
|
|
|
|
|
CSqlite3::CSqlite3()
|
|
|
{
|
|
|
+ wstring folderPath = CSystem::GetProgramDir() + L"\\db";
|
|
|
+ if (!CSystem::IsDirExist(folderPath))
|
|
|
+ {
|
|
|
+ bool flag = CreateDirectory(folderPath.c_str(), NULL);
|
|
|
+ bool a = flag;
|
|
|
+ }
|
|
|
+
|
|
|
//如果没有这个文件,这里会创建这个文件
|
|
|
- wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
|
|
|
- string s_path = CLewaimaiString::UnicodeToUTF8(path);
|
|
|
+ wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
|
|
|
+ string s_path = CLewaimaiString::UnicodeToUTF8(path);
|
|
|
|
|
|
m_rc = sqlite3_open(s_path.c_str(), &m_db);
|
|
|
|
|
|
@@ -23,7 +30,6 @@ CSqlite3::CSqlite3()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
CSqlite3::~CSqlite3()
|
|
|
{
|
|
|
if(m_db != NULL)
|
|
|
@@ -141,8 +147,8 @@ bool CSqlite3::InitConfig()
|
|
|
stmt = NULL;
|
|
|
|
|
|
sql = "CREATE TABLE pos_chufang_printer(" \
|
|
|
- "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
|
|
|
- "date CHAR(100) NOT NULL," \
|
|
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
|
|
|
+ "date CHAR(100) NOT NULL," \
|
|
|
"name CHAR(100) NOT NULL," \
|
|
|
"ip CHAR(100) NOT NULL," \
|
|
|
"guige CHAR(100) NOT NULL," \
|
|
|
@@ -155,7 +161,7 @@ bool CSqlite3::InitConfig()
|
|
|
//执行该语句
|
|
|
if(sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
{
|
|
|
- std::string err = sqlite3_errmsg(m_db);
|
|
|
+ std::string err = sqlite3_errmsg(m_db);
|
|
|
LOG_INFO("create table fail: " << err.c_str());
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
@@ -190,7 +196,7 @@ bool CSqlite3::InitConfig()
|
|
|
{
|
|
|
while(sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
{
|
|
|
- std::string date = (char*)sqlite3_column_text(stmt, 1);
|
|
|
+ std::string date = (char*)sqlite3_column_text(stmt, 1);
|
|
|
std::string name = (char*)sqlite3_column_text(stmt, 2);
|
|
|
std::string ip = (char*)sqlite3_column_text(stmt, 3);
|
|
|
std::string guige = (char*)sqlite3_column_text(stmt, 4);
|
|
|
@@ -198,7 +204,7 @@ bool CSqlite3::InitConfig()
|
|
|
std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
|
|
|
std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
|
|
|
|
|
|
- //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
+ //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
|
|
|
}
|
|
|
|
|
|
@@ -214,84 +220,84 @@ bool CSqlite3::InitConfig()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
|
|
|
+ sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
|
|
|
|
|
|
- //读取厨房打印机的参数
|
|
|
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
- {
|
|
|
- if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
- {
|
|
|
- int count = sqlite3_column_int(stmt, 0);
|
|
|
-
|
|
|
- if (count == 0)
|
|
|
- {
|
|
|
- //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- stmt = NULL;
|
|
|
-
|
|
|
- sql = "CREATE TABLE pos_user(" \
|
|
|
- "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
|
|
|
- "username CHAR(100) NOT NULL," \
|
|
|
- "password CHAR(100) NOT NULL);";
|
|
|
-
|
|
|
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
- {
|
|
|
- //执行该语句
|
|
|
- if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
- {
|
|
|
- std::string err = sqlite3_errmsg(m_db);
|
|
|
- LOG_INFO("create table fail: " << err.c_str());
|
|
|
-
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- //走到这里就是表创建成功了
|
|
|
- LOG_INFO("create table success");
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- }
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
-
|
|
|
- sqlite3_finalize(stmt);
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- //说明已经有这个表了,就不用再创建了
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- }
|
|
|
-
|
|
|
- std::string sql = "SELECT * FROM pos_user;";
|
|
|
- sqlite3_stmt * stmt = NULL;
|
|
|
-
|
|
|
- if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
- {
|
|
|
- while (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
- {
|
|
|
- std::string username = (char*)sqlite3_column_text(stmt, 1);
|
|
|
- std::string password = (char*)sqlite3_column_text(stmt, 2);
|
|
|
-
|
|
|
- //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
- CSetting::SetUser(username, password);
|
|
|
- }
|
|
|
-
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- }
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- //异常情况
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ //读取厨房打印机的参数
|
|
|
+ if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if(sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if(count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "CREATE TABLE pos_user(" \
|
|
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
|
|
|
+ "username CHAR(100) NOT NULL," \
|
|
|
+ "password CHAR(100) NOT NULL);";
|
|
|
+
|
|
|
+ if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ //执行该语句
|
|
|
+ if(sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ std::string err = sqlite3_errmsg(m_db);
|
|
|
+ LOG_INFO("create table fail: " << err.c_str());
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ LOG_INFO("create table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //说明已经有这个表了,就不用再创建了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string sql = "SELECT * FROM pos_user;";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ while(sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ std::string username = (char*)sqlite3_column_text(stmt, 1);
|
|
|
+ std::string password = (char*)sqlite3_column_text(stmt, 2);
|
|
|
+
|
|
|
+ //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
+ CSetting::SetUser(username, password);
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
@@ -326,61 +332,61 @@ bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
|
|
|
|
|
|
bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
|
|
|
{
|
|
|
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
|
|
|
+ int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
|
|
|
|
|
|
- std::string sql = "delete from pos_chufang_printer;";
|
|
|
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+ std::string sql = "delete from pos_chufang_printer;";
|
|
|
+ result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
|
|
|
- for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
|
|
|
- {
|
|
|
- std::string date = (*it).date;
|
|
|
- std::string name = (*it).name;
|
|
|
- std::string ip = (*it).ip;
|
|
|
- std::string guige = (*it).guige;
|
|
|
- std::string fendan = (*it).fendan;
|
|
|
- std::string fenlei = (*it).fenlei;
|
|
|
- std::string fenlei_ids = (*it).fenlei_ids;
|
|
|
-
|
|
|
- sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
|
|
|
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
- }
|
|
|
+ for(std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
|
|
|
+ {
|
|
|
+ std::string date = (*it).date;
|
|
|
+ std::string name = (*it).name;
|
|
|
+ std::string ip = (*it).ip;
|
|
|
+ std::string guige = (*it).guige;
|
|
|
+ std::string fendan = (*it).fendan;
|
|
|
+ std::string fenlei = (*it).fenlei;
|
|
|
+ std::string fenlei_ids = (*it).fenlei_ids;
|
|
|
+
|
|
|
+ sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
|
|
|
+ result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+ }
|
|
|
|
|
|
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
|
|
|
+ result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
|
|
|
|
|
|
- if (result == SQLITE_OK)
|
|
|
- {
|
|
|
- LOG_INFO("save params success");
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if(result == SQLITE_OK)
|
|
|
+ {
|
|
|
+ LOG_INFO("save params success");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- LOG_INFO("save params fail");
|
|
|
- return false;
|
|
|
+ LOG_INFO("save params fail");
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
bool CSqlite3::SaveUsers(std::map<string, string> users)
|
|
|
{
|
|
|
- int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
|
|
|
+ int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
|
|
|
|
|
|
- std::string sql = "delete from pos_user;";
|
|
|
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+ std::string sql = "delete from pos_user;";
|
|
|
+ result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
|
|
|
- for (std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
|
|
|
- {
|
|
|
- std::string name = it->first;
|
|
|
- std::string password = it->second;
|
|
|
+ for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
|
|
|
+ {
|
|
|
+ std::string name = it->first;
|
|
|
+ std::string password = it->second;
|
|
|
|
|
|
- sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
|
|
|
- result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
- }
|
|
|
+ sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
|
|
|
+ result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
|
|
|
+ }
|
|
|
|
|
|
- result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
|
|
|
+ result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
|
|
|
|
|
|
- if (result == SQLITE_OK)
|
|
|
- {
|
|
|
- LOG_INFO("save params success");
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if(result == SQLITE_OK)
|
|
|
+ {
|
|
|
+ LOG_INFO("save params success");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- LOG_INFO("save params fail");
|
|
|
- return false;
|
|
|
+ LOG_INFO("save params fail");
|
|
|
+ return false;
|
|
|
}
|