CSqlite3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include "../pch/pch.h"
  2. #include "CSqlite3.h"
  3. #include "CSetting.h"
  4. CSqlite3::CSqlite3()
  5. {
  6. //如果没有这个文件,这里会创建这个文件
  7. m_rc = sqlite3_open("db/pos.db", &m_db);
  8. if(m_rc)
  9. {
  10. LOG_INFO("Can't open database: " << sqlite3_errmsg(m_db));
  11. return;
  12. }
  13. else
  14. {
  15. LOG_INFO("Opened database successfully");
  16. }
  17. }
  18. CSqlite3::~CSqlite3()
  19. {
  20. if(m_db != NULL)
  21. {
  22. sqlite3_close(m_db);
  23. }
  24. }
  25. bool CSqlite3::InitConfig()
  26. {
  27. //检查有没有pos_config这个表,如果没有就创建
  28. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
  29. sqlite3_stmt * stmt = NULL;
  30. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  31. {
  32. if(sqlite3_step(stmt) == SQLITE_ROW)
  33. {
  34. int count = sqlite3_column_int(stmt, 0);
  35. if(count == 0)
  36. {
  37. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  38. sqlite3_finalize(stmt);
  39. stmt = NULL;
  40. sql = "CREATE TABLE pos_config(" \
  41. "name CHAR(100) UNIQUE NOT NULL," \
  42. "value CHAR(2000) NOT NULL);";
  43. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  44. {
  45. //执行该语句
  46. if(sqlite3_step(stmt) != SQLITE_DONE)
  47. {
  48. LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
  49. sqlite3_finalize(stmt);
  50. return false;
  51. }
  52. //走到这里就是表创建成功了
  53. LOG_INFO("create table success");
  54. sqlite3_finalize(stmt);
  55. }
  56. else
  57. {
  58. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  59. sqlite3_finalize(stmt);
  60. return false;
  61. }
  62. }
  63. else
  64. {
  65. //说明已经有这个表了,就不用再创建了
  66. sqlite3_finalize(stmt);
  67. }
  68. std::string sql = "SELECT * FROM pos_config;";
  69. sqlite3_stmt * stmt = NULL;
  70. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  71. {
  72. while(sqlite3_step(stmt) == SQLITE_ROW)
  73. {
  74. std::string name = (char*)sqlite3_column_text(stmt, 0);
  75. std::string value = (char*)sqlite3_column_text(stmt, 1);
  76. CSetting::SetParam(name, value, false);
  77. }
  78. sqlite3_finalize(stmt);
  79. CSetting::Init();
  80. }
  81. else
  82. {
  83. //异常情况
  84. sqlite3_finalize(stmt);
  85. return false;
  86. }
  87. }
  88. else
  89. {
  90. //异常情况
  91. sqlite3_finalize(stmt);
  92. return false;
  93. }
  94. }
  95. else
  96. {
  97. //异常情况
  98. sqlite3_finalize(stmt);
  99. return false;
  100. }
  101. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
  102. //读取厨房打印机的参数
  103. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  104. {
  105. if(sqlite3_step(stmt) == SQLITE_ROW)
  106. {
  107. int count = sqlite3_column_int(stmt, 0);
  108. if(count == 0)
  109. {
  110. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  111. sqlite3_finalize(stmt);
  112. stmt = NULL;
  113. sql = "CREATE TABLE pos_chufang_printer(" \
  114. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  115. "date CHAR(100) NOT NULL," \
  116. "name CHAR(100) NOT NULL," \
  117. "ip CHAR(100) NOT NULL," \
  118. "guige CHAR(100) NOT NULL," \
  119. "fendan CHAR(100) NOT NULL," \
  120. "fenlei CHAR(100) NOT NULL," \
  121. "fenlei_ids CHAR(2000) );";
  122. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  123. {
  124. //执行该语句
  125. if(sqlite3_step(stmt) != SQLITE_DONE)
  126. {
  127. std::string err = sqlite3_errmsg(m_db);
  128. LOG_INFO("create table fail: " << err.c_str());
  129. sqlite3_finalize(stmt);
  130. return false;
  131. }
  132. //走到这里就是表创建成功了
  133. LOG_INFO("create table success");
  134. sqlite3_finalize(stmt);
  135. }
  136. else
  137. {
  138. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  139. sqlite3_finalize(stmt);
  140. return false;
  141. }
  142. }
  143. else
  144. {
  145. //说明已经有这个表了,就不用再创建了
  146. sqlite3_finalize(stmt);
  147. }
  148. std::string sql = "SELECT * FROM pos_chufang_printer;";
  149. sqlite3_stmt * stmt = NULL;
  150. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  151. {
  152. while(sqlite3_step(stmt) == SQLITE_ROW)
  153. {
  154. std::string date = (char*)sqlite3_column_text(stmt, 1);
  155. std::string name = (char*)sqlite3_column_text(stmt, 2);
  156. std::string ip = (char*)sqlite3_column_text(stmt, 3);
  157. std::string guige = (char*)sqlite3_column_text(stmt, 4);
  158. std::string fendan = (char*)sqlite3_column_text(stmt, 5);
  159. std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
  160. std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
  161. //这里仅仅是把数据库内容读到内存,所以之类用false
  162. CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
  163. }
  164. sqlite3_finalize(stmt);
  165. }
  166. else
  167. {
  168. //异常情况
  169. sqlite3_finalize(stmt);
  170. return false;
  171. }
  172. }
  173. }
  174. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
  175. //读取厨房打印机的参数
  176. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  177. {
  178. if (sqlite3_step(stmt) == SQLITE_ROW)
  179. {
  180. int count = sqlite3_column_int(stmt, 0);
  181. if (count == 0)
  182. {
  183. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  184. sqlite3_finalize(stmt);
  185. stmt = NULL;
  186. sql = "CREATE TABLE pos_user(" \
  187. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  188. "username CHAR(100) NOT NULL," \
  189. "password CHAR(100) NOT NULL);";
  190. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  191. {
  192. //执行该语句
  193. if (sqlite3_step(stmt) != SQLITE_DONE)
  194. {
  195. std::string err = sqlite3_errmsg(m_db);
  196. LOG_INFO("create table fail: " << err.c_str());
  197. sqlite3_finalize(stmt);
  198. return false;
  199. }
  200. //走到这里就是表创建成功了
  201. LOG_INFO("create table success");
  202. sqlite3_finalize(stmt);
  203. }
  204. else
  205. {
  206. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  207. sqlite3_finalize(stmt);
  208. return false;
  209. }
  210. }
  211. else
  212. {
  213. //说明已经有这个表了,就不用再创建了
  214. sqlite3_finalize(stmt);
  215. }
  216. std::string sql = "SELECT * FROM pos_user;";
  217. sqlite3_stmt * stmt = NULL;
  218. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  219. {
  220. while (sqlite3_step(stmt) == SQLITE_ROW)
  221. {
  222. std::string username = (char*)sqlite3_column_text(stmt, 1);
  223. std::string password = (char*)sqlite3_column_text(stmt, 2);
  224. //这里仅仅是把数据库内容读到内存,所以之类用false
  225. CSetting::SetUser(username, password);
  226. }
  227. sqlite3_finalize(stmt);
  228. }
  229. else
  230. {
  231. //异常情况
  232. sqlite3_finalize(stmt);
  233. return false;
  234. }
  235. }
  236. }
  237. return true;
  238. }
  239. bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
  240. {
  241. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  242. std::string sql = "delete from pos_config;";
  243. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  244. for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
  245. {
  246. std::string name = it->first;
  247. std::string value = it->second;
  248. sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
  249. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  250. }
  251. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  252. if(result == SQLITE_OK)
  253. {
  254. LOG_INFO("save params success");
  255. return true;
  256. }
  257. LOG_INFO("save params fail");
  258. return false;
  259. }
  260. bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
  261. {
  262. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  263. std::string sql = "delete from pos_chufang_printer;";
  264. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  265. for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
  266. {
  267. std::string date = (*it).date;
  268. std::string name = (*it).name;
  269. std::string ip = (*it).ip;
  270. std::string guige = (*it).guige;
  271. std::string fendan = (*it).fendan;
  272. std::string fenlei = (*it).fenlei;
  273. std::string fenlei_ids = (*it).fenlei_ids;
  274. sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
  275. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  276. }
  277. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  278. if (result == SQLITE_OK)
  279. {
  280. LOG_INFO("save params success");
  281. return true;
  282. }
  283. LOG_INFO("save params fail");
  284. return false;
  285. }
  286. bool CSqlite3::SaveUsers(std::map<string, string> users)
  287. {
  288. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  289. std::string sql = "delete from pos_user;";
  290. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  291. for (std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
  292. {
  293. std::string name = it->first;
  294. std::string password = it->second;
  295. sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
  296. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  297. }
  298. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  299. if (result == SQLITE_OK)
  300. {
  301. LOG_INFO("save params success");
  302. return true;
  303. }
  304. LOG_INFO("save params fail");
  305. return false;
  306. }