CSqlite3.cpp 11 KB

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