SQLite3Interface.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /************************************************************************/
  2. /*
  3. 文件名称: SQLite3Interface.h
  4. 文件标识:
  5. 内容摘要: SQLite3 封装类;
  6. 其它说明: 无
  7. 当前版本: V 0.1
  8. 作 者: Jeff
  9. 完成日期: 2015年04月06日
  10. 修改记录1:
  11. 修改日期:-
  12. 版 本 号:-
  13. 修 改 人:-
  14. 修改内容:-
  15. */
  16. /************************************************************************/
  17. #include "StdAfx.h"
  18. #include "SQLite3Interface.h"
  19. CSQLite3Interface::CSQLite3Interface(void)
  20. {
  21. m_pdb = NULL;
  22. m_bOpen = false;
  23. }
  24. CSQLite3Interface::~CSQLite3Interface(void)
  25. {
  26. if ( m_pdb )
  27. {
  28. sqlite3_close(m_pdb);
  29. m_pdb = NULL;
  30. m_bOpen = false;
  31. }
  32. }
  33. int CSQLite3Interface::closedb()
  34. {
  35. if ( m_pdb )
  36. {
  37. sqlite3_close(m_pdb);
  38. m_pdb = NULL;
  39. m_bOpen = false;
  40. }
  41. return 0;
  42. }
  43. /************************************************************************/
  44. /*
  45. Jeff 2015.04.05
  46. 函数:excute
  47. 描述:执行SQL语句;
  48. 参数:
  49. pszexcu 要执行的SQL语句:
  50. 返回:成功返回0,错误返回其他值;
  51. 注意:调用的sqliteAPI打开中文名时需要转为utf8字符;
  52. 调用约定:
  53. */
  54. /************************************************************************/
  55. int CSQLite3Interface::excute(const TCHAR* pszexcu)
  56. {
  57. int sqlite_error = 0;
  58. char szexcu[MAX_PATH] = {0};
  59. #ifdef UNICODE
  60. // 将路径由unicode转为utf8;
  61. int len = unicode2uft8(pszexcu,szexcu);
  62. if ( len == 0)
  63. return -1;
  64. #else
  65. // 将路径由ascii转为utf8;
  66. int len = ascii2utf8(pszexcu,szexcu);
  67. if ( len == 0)
  68. return -1;
  69. #endif
  70. char *pExp = NULL; // 错误信息;
  71. sqlite_error = sqlite3_exec(m_pdb,szexcu,NULL,0,&pExp);
  72. if ( sqlite_error != SQLITE_OK)
  73. {
  74. // 插入数据失败;
  75. }
  76. else
  77. {
  78. // 插入数据成功;
  79. }
  80. if(pExp) sqlite3_free(pExp);
  81. return sqlite_error;
  82. }
  83. /************************************************************************/
  84. /*
  85. Jeff 2015.04.05
  86. 函数:opendb
  87. 描述:打开sqlite3数据库;
  88. 参数:
  89. pszdb 要打开的文件路径名:
  90. 返回:成功返回0,错误返回其他值;
  91. 注意:调用的sqliteAPI打开中文名时需要转为utf8字符;
  92. 调用约定:
  93. */
  94. /************************************************************************/
  95. int CSQLite3Interface::opendb(const TCHAR *pszdb)
  96. {
  97. // 是否已经打开了;
  98. if(m_pdb) return 0;
  99. int sqlite_error = 0;
  100. char szdb[MAX_PATH] = {0};
  101. #ifdef UNICODE
  102. // 将路径由unicode转为utf8;
  103. int len = unicode2uft8(pszdb,szdb);
  104. if ( len == 0)
  105. return -1;
  106. #else
  107. // 将路径由ascii转为utf8;
  108. int len = ascii2utf8(pszdb,szdb);
  109. if ( len == 0)
  110. return -1;
  111. #endif
  112. // 打开数据库,若不存在则创建;
  113. sqlite_error = sqlite3_open(szdb,&m_pdb);
  114. if ( sqlite_error != SQLITE_OK)
  115. {
  116. // 打开或创建数据库失败;
  117. sqlite3_close(m_pdb);
  118. m_pdb = NULL;
  119. m_bOpen = false;
  120. }
  121. else
  122. {
  123. // 打开或创建数据库成功;
  124. m_bOpen = true;
  125. }
  126. return sqlite_error;
  127. }
  128. /************************************************************************/
  129. /*
  130. Jeff 2015.04.05
  131. 函数:creattbl
  132. 描述:创建sqlite3表;
  133. 参数:
  134. pszcreate 创建表的SQL语句:
  135. 返回:成功返回0,错误返回其他值;
  136. 注意:调用的sqliteAPI时需要转为utf8字符;
  137. 调用约定:
  138. */
  139. /************************************************************************/
  140. int CSQLite3Interface::creattbl(const TCHAR *pszcreate)
  141. {
  142. int sqlite_error = 0;
  143. char szSQL[MAX_PATH] = {0};
  144. #ifdef UNICODE
  145. // 将路径由unicode转为utf8;
  146. int len = unicode2uft8(pszcreate,szSQL);
  147. if ( len == 0)
  148. return -1;
  149. #else
  150. // 将路径由ascii转为utf8;
  151. int len = ascii2utf8(pszcreate,szSQL);
  152. if ( len == 0)
  153. return -1;
  154. #endif
  155. char *pExp = NULL; // 错误信息;
  156. sqlite_error = sqlite3_exec(m_pdb,szSQL,NULL,0,&pExp);
  157. if ( sqlite_error != SQLITE_OK)
  158. {
  159. // 创建表失败;
  160. }
  161. else
  162. {
  163. // 成功创建表;
  164. }
  165. if(pExp) sqlite3_free(pExp);
  166. return sqlite_error;
  167. }
  168. /************************************************************************/
  169. /*
  170. Jeff 2015.04.05
  171. 函数:isnerttbl
  172. 描述:插入数据到sqlite3表;
  173. 参数:
  174. pszInsert 插入表的SQL语句:
  175. 返回:成功返回0,错误返回其他值;
  176. 注意:调用的sqliteAPI时需要转为utf8字符;
  177. 调用约定:
  178. */
  179. /************************************************************************/
  180. int CSQLite3Interface::isnerttbl(const TCHAR* pszInsert)
  181. {
  182. int sqlite_error = 0;
  183. char szInsert[MAX_PATH] = {0};
  184. #ifdef UNICODE
  185. // 将路径由unicode转为utf8;
  186. int len = unicode2uft8(pszInsert,szInsert);
  187. if ( len == 0)
  188. return -1;
  189. #else
  190. // 将路径由ascii转为utf8;
  191. int len = ascii2utf8(pszInsert,szInsert);
  192. if ( len == 0)
  193. return -1;
  194. #endif
  195. char *pExp = NULL; // 错误信息;
  196. sqlite_error = sqlite3_exec(m_pdb,szInsert,NULL,0,&pExp);
  197. if ( sqlite_error != SQLITE_OK)
  198. {
  199. // 插入数据失败;
  200. }
  201. else
  202. {
  203. // 插入数据成功;
  204. }
  205. if(pExp) sqlite3_free(pExp);
  206. return sqlite_error;
  207. }
  208. int CSQLite3Interface::gettbl_remoteinfo(const TCHAR* pszSelect,vector<STRemoteInfo> &vtRemoteInfo)
  209. {
  210. int sqlite_error = 0;
  211. char szSelect[MAX_PATH] = {0};
  212. #ifdef UNICODE
  213. // 将路径由unicode转为utf8;
  214. int len = unicode2uft8(pszSelect,szSelect);
  215. if ( len == 0)
  216. return -1;
  217. #else
  218. // 将路径由ascii转为utf8;
  219. int len = ascii2utf8(pszSelect,szSelect);
  220. if ( len == 0)
  221. return -1;
  222. #endif
  223. char *pExp = NULL; // 错误信息;
  224. int nRow = 0;
  225. int nColumn = 0;
  226. char **ptbldata = NULL;
  227. sqlite_error = sqlite3_get_table(m_pdb,szSelect,&ptbldata,&nRow,&nColumn,&pExp);
  228. if ( sqlite_error != SQLITE_OK)
  229. {
  230. // 获取表数据失败;
  231. }
  232. else
  233. {
  234. // 获取表数据成功;
  235. nRow += 1;
  236. for ( int i = 1; i < nRow; i++)
  237. {
  238. // 插入数据时使用的是utf8字符,获取时需要还原回unicode或ascii;
  239. STRemoteInfo tagInfo;
  240. tagInfo.bInUse = atoi(ptbldata[i*nColumn]);
  241. #ifdef UNICODE
  242. utf82unicode(ptbldata[i*nColumn+1],tagInfo.szBranch);
  243. utf82unicode(ptbldata[i*nColumn+2],tagInfo.szIP);
  244. utf82unicode(ptbldata[i*nColumn+3],tagInfo.szDoMain);
  245. #else
  246. utf82ascii(ptbldata[i*nColumn+1],tagInfo.szBranch);
  247. utf82ascii(ptbldata[i*nColumn+2],tagInfo.szIP);
  248. utf82ascii(ptbldata[i*nColumn+3],tagInfo.szDoMain);
  249. #endif
  250. vtRemoteInfo.push_back(tagInfo);
  251. }
  252. }
  253. // 使用sqlite3_get_table后要释放;
  254. if(ptbldata) sqlite3_free_table(ptbldata);
  255. // 释放错误信息内存;
  256. if(pExp) sqlite3_free(pExp);
  257. return sqlite_error;
  258. }
  259. int CSQLite3Interface::removetbl(const TCHAR* pszdel)
  260. {
  261. int sqlite_error = 0;
  262. char szdel[MAX_PATH] = {0};
  263. #ifdef UNICODE
  264. // 将路径由unicode转为utf8;
  265. int len = unicode2uft8(pszdel,szdel);
  266. if ( len == 0)
  267. return -1;
  268. #else
  269. // 将路径由ascii转为utf8;
  270. int len = ascii2utf8(pszdel,szdel);
  271. if ( len == 0)
  272. return -1;
  273. #endif
  274. char *pExp = NULL; // 错误信息;
  275. sqlite_error = sqlite3_exec(m_pdb,szdel,NULL,0,&pExp);
  276. if ( sqlite_error != SQLITE_OK)
  277. {
  278. // 插入数据失败;
  279. }
  280. else
  281. {
  282. // 插入数据成功;
  283. }
  284. if(pExp) sqlite3_free(pExp);
  285. return sqlite_error;
  286. }