CSqlite3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. }
  82. else
  83. {
  84. //异常情况
  85. sqlite3_finalize(stmt);
  86. return false;
  87. }
  88. }
  89. else
  90. {
  91. //异常情况
  92. sqlite3_finalize(stmt);
  93. return false;
  94. }
  95. }
  96. else
  97. {
  98. //异常情况
  99. sqlite3_finalize(stmt);
  100. return false;
  101. }
  102. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
  103. //读取厨房打印机的参数
  104. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  105. {
  106. if(sqlite3_step(stmt) == SQLITE_ROW)
  107. {
  108. int count = sqlite3_column_int(stmt, 0);
  109. if(count == 0)
  110. {
  111. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  112. sqlite3_finalize(stmt);
  113. stmt = NULL;
  114. sql = "CREATE TABLE pos_chufang_printer(" \
  115. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  116. "date CHAR(100) NOT NULL," \
  117. "name CHAR(100) NOT NULL," \
  118. "ip CHAR(100) NOT NULL," \
  119. "guige CHAR(100) NOT NULL," \
  120. "fendan CHAR(100) NOT NULL," \
  121. "fenlei CHAR(100) NOT NULL," \
  122. "fenlei_ids CHAR(2000) );";
  123. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  124. {
  125. //执行该语句
  126. if(sqlite3_step(stmt) != SQLITE_DONE)
  127. {
  128. std::string err = sqlite3_errmsg(m_db);
  129. LOG_INFO("create table fail: " << err.c_str());
  130. sqlite3_finalize(stmt);
  131. return false;
  132. }
  133. //走到这里就是表创建成功了
  134. LOG_INFO("create table success");
  135. sqlite3_finalize(stmt);
  136. }
  137. else
  138. {
  139. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  140. sqlite3_finalize(stmt);
  141. return false;
  142. }
  143. }
  144. else
  145. {
  146. //说明已经有这个表了,就不用再创建了
  147. sqlite3_finalize(stmt);
  148. }
  149. std::string sql = "SELECT * FROM pos_chufang_printer;";
  150. sqlite3_stmt * stmt = NULL;
  151. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  152. {
  153. while(sqlite3_step(stmt) == SQLITE_ROW)
  154. {
  155. std::string date = (char*)sqlite3_column_text(stmt, 1);
  156. std::string name = (char*)sqlite3_column_text(stmt, 2);
  157. std::string ip = (char*)sqlite3_column_text(stmt, 3);
  158. std::string guige = (char*)sqlite3_column_text(stmt, 4);
  159. std::string fendan = (char*)sqlite3_column_text(stmt, 5);
  160. std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
  161. std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
  162. //这里仅仅是把数据库内容读到内存,所以之类用false
  163. CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
  164. }
  165. sqlite3_finalize(stmt);
  166. }
  167. else
  168. {
  169. //异常情况
  170. sqlite3_finalize(stmt);
  171. return false;
  172. }
  173. }
  174. }
  175. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
  176. //读取厨房打印机的参数
  177. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  178. {
  179. if (sqlite3_step(stmt) == SQLITE_ROW)
  180. {
  181. int count = sqlite3_column_int(stmt, 0);
  182. if (count == 0)
  183. {
  184. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  185. sqlite3_finalize(stmt);
  186. stmt = NULL;
  187. sql = "CREATE TABLE pos_user(" \
  188. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  189. "username CHAR(100) NOT NULL," \
  190. "password CHAR(100) NOT NULL);";
  191. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  192. {
  193. //执行该语句
  194. if (sqlite3_step(stmt) != SQLITE_DONE)
  195. {
  196. std::string err = sqlite3_errmsg(m_db);
  197. LOG_INFO("create table fail: " << err.c_str());
  198. sqlite3_finalize(stmt);
  199. return false;
  200. }
  201. //走到这里就是表创建成功了
  202. LOG_INFO("create table success");
  203. sqlite3_finalize(stmt);
  204. }
  205. else
  206. {
  207. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  208. sqlite3_finalize(stmt);
  209. return false;
  210. }
  211. }
  212. else
  213. {
  214. //说明已经有这个表了,就不用再创建了
  215. sqlite3_finalize(stmt);
  216. }
  217. std::string sql = "SELECT * FROM pos_user;";
  218. sqlite3_stmt * stmt = NULL;
  219. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  220. {
  221. while (sqlite3_step(stmt) == SQLITE_ROW)
  222. {
  223. std::string username = (char*)sqlite3_column_text(stmt, 1);
  224. std::string password = (char*)sqlite3_column_text(stmt, 2);
  225. //这里仅仅是把数据库内容读到内存,所以之类用false
  226. CSetting::SetUser(username, password);
  227. }
  228. sqlite3_finalize(stmt);
  229. }
  230. else
  231. {
  232. //异常情况
  233. sqlite3_finalize(stmt);
  234. return false;
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
  241. {
  242. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  243. std::string sql = "delete from pos_config;";
  244. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  245. for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
  246. {
  247. std::string name = it->first;
  248. std::string value = it->second;
  249. sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
  250. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  251. }
  252. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  253. if(result == SQLITE_OK)
  254. {
  255. LOG_INFO("save params success");
  256. return true;
  257. }
  258. LOG_INFO("save params fail");
  259. return false;
  260. }
  261. bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
  262. {
  263. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  264. std::string sql = "delete from pos_chufang_printer;";
  265. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  266. for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
  267. {
  268. std::string date = (*it).date;
  269. std::string name = (*it).name;
  270. std::string ip = (*it).ip;
  271. std::string guige = (*it).guige;
  272. std::string fendan = (*it).fendan;
  273. std::string fenlei = (*it).fenlei;
  274. std::string fenlei_ids = (*it).fenlei_ids;
  275. sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
  276. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  277. }
  278. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  279. if (result == SQLITE_OK)
  280. {
  281. LOG_INFO("save params success");
  282. return true;
  283. }
  284. LOG_INFO("save params fail");
  285. return false;
  286. }
  287. bool CSqlite3::SaveUsers(std::map<string, string> users)
  288. {
  289. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  290. std::string sql = "delete from pos_user;";
  291. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  292. for (std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
  293. {
  294. std::string name = it->first;
  295. std::string password = it->second;
  296. sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
  297. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  298. }
  299. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  300. if (result == SQLITE_OK)
  301. {
  302. LOG_INFO("save params success");
  303. return true;
  304. }
  305. LOG_INFO("save params fail");
  306. return false;
  307. }