|
|
@@ -44,102 +44,129 @@ CSqlite3::~CSqlite3()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool CSqlite3::InitConfig()
|
|
|
+bool CSqlite3::InitDB()
|
|
|
{
|
|
|
- //检查有没有pos_config这个表,如果没有就创建
|
|
|
- std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
|
|
|
- sqlite3_stmt * stmt = NULL;
|
|
|
+ //先处理pos_config表
|
|
|
+ bool ret = this->InitPosConfig();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ 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_config(" \
|
|
|
- "name CHAR(100) UNIQUE NOT NULL," \
|
|
|
- "value CHAR(2000) NOT NULL);";
|
|
|
-
|
|
|
- if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
- {
|
|
|
- //执行该语句
|
|
|
- if(sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
- {
|
|
|
- LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
-
|
|
|
- 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_config;";
|
|
|
- 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 name = (char*)sqlite3_column_text(stmt, 0);
|
|
|
- std::string value = (char*)sqlite3_column_text(stmt, 1);
|
|
|
-
|
|
|
- CSetting::SetParam(name, value, false);
|
|
|
- }
|
|
|
-
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- }
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- //异常情况
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else
|
|
|
- {
|
|
|
- //异常情况
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
+ //再处理pos_chufang_printer表
|
|
|
+ ret = this->InitPosChufangPrinter();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- else
|
|
|
- {
|
|
|
- //异常情况
|
|
|
- sqlite3_finalize(stmt);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ //再处理pos_user表
|
|
|
+ ret = this->InitPosUser();
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitPosConfig()
|
|
|
+{
|
|
|
+ //检查有没有pos_config这个表,如果没有就创建
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
|
|
|
- //读取厨房打印机的参数
|
|
|
+ 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_config(" \
|
|
|
+ "name CHAR(100) UNIQUE NOT NULL," \
|
|
|
+ "value CHAR(2000) NOT NULL);";
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ //执行该语句
|
|
|
+ if (sqlite3_step(stmt) != SQLITE_DONE)
|
|
|
+ {
|
|
|
+ LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ 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_config;";
|
|
|
+ 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 name = (char*)sqlite3_column_text(stmt, 0);
|
|
|
+ std::string value = (char*)sqlite3_column_text(stmt, 1);
|
|
|
+
|
|
|
+ CSetting::SetParam(name, value, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool CSqlite3::InitPosChufangPrinter()
|
|
|
+{
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ //读取厨房打印机的参数
|
|
|
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
{
|
|
|
if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
@@ -178,7 +205,6 @@ bool CSqlite3::InitConfig()
|
|
|
LOG_INFO("create table success");
|
|
|
sqlite3_finalize(stmt);
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
@@ -188,7 +214,6 @@ bool CSqlite3::InitConfig()
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
//说明已经有这个表了,就不用再创建了
|
|
|
@@ -233,17 +258,15 @@ bool CSqlite3::InitConfig()
|
|
|
LOG_INFO("alter table success");
|
|
|
sqlite3_finalize(stmt);
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
|
|
- return false;;
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
LOG_INFO("fount field");
|
|
|
@@ -253,7 +276,6 @@ bool CSqlite3::InitConfig()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
//异常情况
|
|
|
@@ -308,7 +330,6 @@ bool CSqlite3::InitConfig()
|
|
|
return false;;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
LOG_INFO("fount field");
|
|
|
@@ -318,7 +339,6 @@ bool CSqlite3::InitConfig()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
//异常情况
|
|
|
@@ -347,7 +367,6 @@ bool CSqlite3::InitConfig()
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
}
|
|
|
-
|
|
|
else
|
|
|
{
|
|
|
//异常情况
|
|
|
@@ -357,86 +376,89 @@ bool CSqlite3::InitConfig()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
|
|
|
+ return true;
|
|
|
+}
|
|
|
|
|
|
- //读取厨房打印机的参数
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+bool CSqlite3::InitPosUser()
|
|
|
+{
|
|
|
+ std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
|
|
|
+ sqlite3_stmt * stmt = NULL;
|
|
|
+
|
|
|
+ //读取厨房打印机的参数
|
|
|
+ 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;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
|