|
|
@@ -140,87 +140,222 @@ bool CSqlite3::InitConfig()
|
|
|
sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
|
|
|
|
|
|
//读取厨房打印机的参数
|
|
|
- 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_chufang_printer(" \
|
|
|
- "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," \
|
|
|
- "fendan 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_chufang_printer;";
|
|
|
- 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 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);
|
|
|
- std::string fendan = (char*)sqlite3_column_text(stmt, 5);
|
|
|
-
|
|
|
- //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
- CSetting::AddChufangPrinter(date, name, ip, guige, fendan, false);
|
|
|
- }
|
|
|
-
|
|
|
- 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_chufang_printer(" \
|
|
|
+ "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," \
|
|
|
+ "fendan CHAR(100) NOT NULL," \
|
|
|
+ "fenlei CHAR(100) NOT NULL," \
|
|
|
+ "fenlei_ids CHAR(2000) );";
|
|
|
+
|
|
|
+ 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_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei%';";
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个字段,准备添加字段
|
|
|
+ LOG_INFO("not fount field");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "ALTER TABLE pos_chufang_printer " \
|
|
|
+ "add fenlei CHAR(100) NOT NULL DEFAULT '0';";
|
|
|
+
|
|
|
+ LOG_INFO("sql:" << sql.c_str());
|
|
|
+
|
|
|
+ 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("alter table fail: " << err.c_str());
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ LOG_INFO("alter table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("fount field");
|
|
|
+
|
|
|
+ //说明已经有这2个字段了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei_ids%';";
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ if (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ int count = sqlite3_column_int(stmt, 0);
|
|
|
+
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ //说明没找到这个字段,准备添加字段
|
|
|
+ LOG_INFO("not fount field");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ sql = "ALTER TABLE pos_chufang_printer " \
|
|
|
+ "add fenlei_ids CHAR(2000) NOT NULL DEFAULT '';";
|
|
|
+
|
|
|
+ LOG_INFO("sql:" << sql.c_str());
|
|
|
+
|
|
|
+ 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("alter table fail: " << err.c_str());
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //走到这里就是表创建成功了
|
|
|
+ LOG_INFO("alter table success");
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+
|
|
|
+ return false;;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("fount field");
|
|
|
+
|
|
|
+ //说明已经有这2个字段了
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string sql = "SELECT * FROM pos_chufang_printer;";
|
|
|
+ stmt = NULL;
|
|
|
+
|
|
|
+ if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
|
|
|
+ {
|
|
|
+ while (sqlite3_step(stmt) == SQLITE_ROW)
|
|
|
+ {
|
|
|
+ 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);
|
|
|
+ std::string fendan = (char*)sqlite3_column_text(stmt, 5);
|
|
|
+ std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
|
|
|
+ std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
|
|
|
+
|
|
|
+ //这里仅仅是把数据库内容读到内存,所以之类用false
|
|
|
+ CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //异常情况
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
|
|
|
|