SQLite3Interface.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "stdafx.h"
  2. #include "SQLite3Interface.h"
  3. CSQLiter3Interface::CSQLiter3Interface()
  4. {
  5. m_pSqli3db = NULL;
  6. }
  7. CSQLiter3Interface::~CSQLiter3Interface()
  8. {
  9. Close();
  10. }
  11. /************************************************************************/
  12. /*
  13. 函数: OpenSQLite3DB
  14. 描述: 打开数据库
  15. 参数:
  16. pDBPath 数据库路径
  17. 返回:
  18. 注意: 0成功, -1失败;
  19. */
  20. /************************************************************************/
  21. int CSQLiter3Interface::OpenSQLite3DB(const char* pDBPath)
  22. {
  23. if(pDBPath == NULL)
  24. return -1;
  25. int sqlite_error = -1;
  26. sqlite_error = sqlite3_open(pDBPath, &m_pSqli3db);
  27. if( sqlite_error == 14 )
  28. return -1;
  29. return 0;
  30. }
  31. /************************************************************************/
  32. /*
  33. 函数: CreateTabel
  34. 描述: 创建表create table remoteinfo([inuse] int not null,[branch] text not null,[ip] text not null,[domain] text not null,primary key([domain]));"
  35. 参数:
  36. 返回:
  37. 注意: 0成功, -1失败;
  38. */
  39. /************************************************************************/
  40. int CSQLiter3Interface::CreateTabel(const char* pSql, CString& strError)
  41. {
  42. if(m_pSqli3db == NULL || pSql == NULL)
  43. return -1;
  44. char* psqlite_error = NULL;
  45. int nRet = sqlite3_exec(m_pSqli3db, pSql, NULL, 0, &psqlite_error);
  46. if(nRet != SQLITE_OK && nRet != 1)
  47. {
  48. /*
  49. WCHAR wszError[512] = {0};
  50. ascii2unicode(psqlite_error, wszError);
  51. strError = wszError;
  52. */
  53. strError = psqlite_error;
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. /************************************************************************/
  59. /*
  60. 函数: Get_ErrMsg
  61. 描述: 获取错误消息
  62. 参数:
  63. 返回:
  64. 注意: 返回错误消息
  65. */
  66. /************************************************************************/
  67. const char* CSQLiter3Interface::Get_ErrMsg()
  68. {
  69. return sqlite3_errmsg(m_pSqli3db);
  70. }
  71. /************************************************************************/
  72. /*
  73. 函数: Exec
  74. 描述: 执行SQL
  75. 参数:
  76. pSql sql语句
  77. 返回:
  78. 注意: 0成功, -1失败;
  79. */
  80. /************************************************************************/
  81. int CSQLiter3Interface::Exec(const char* pSql, CString& strError)
  82. {
  83. if(m_pSqli3db == NULL)
  84. return -1;
  85. char* psqlite_error = NULL;
  86. int sqlite_error = sqlite3_exec(m_pSqli3db, pSql, NULL, 0, &psqlite_error);
  87. if(SQLITE_OK != sqlite_error)
  88. {
  89. #ifdef _UNICODE
  90. WCHAR wszError[512] = {0};
  91. Char2WChar(wszError, psqlite_error);
  92. strError = wszError;
  93. #else
  94. strError = psqlite_error;
  95. #endif
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. /************************************************************************/
  101. /*
  102. 函数: Select
  103. 描述: 表查询
  104. 参数:
  105. const char* pTableName 表名
  106. const char* pFields, 一个字段或多个字段如:字段1,字段2,字段3...
  107. const char* pLimit, where 条件如:字段1='xx',字段2='xxx'
  108. char* pazResult, 返回结果集
  109. int* pnRow, 返回执行行数
  110. int* pnCol, 返回执行列数
  111. CString& strError 如果失败返回错误消息
  112. 返回:
  113. 注意: 0成功, -1失败;
  114. */
  115. /************************************************************************/
  116. int CSQLiter3Interface::Select(const char* pTableName, const char* pFields, const char* pLimit, std::vector<TString>& vValues, CString& strError)
  117. {
  118. if(m_pSqli3db == NULL || pTableName == NULL || pFields == NULL)
  119. return -1;
  120. char* psqlite_error = NULL;
  121. char szSql[MAX_PATH] = {0};
  122. if(pLimit == NULL || strcmp(pLimit, "") == 0)
  123. sprintf(szSql, "select %s from %s;", pFields, pTableName);
  124. else
  125. sprintf(szSql, "select %s from %s where %s;", pFields, pTableName, pLimit);
  126. int nRow(0), nCol(0);
  127. char** pazResult = NULL;
  128. int sqlite_error = sqlite3_get_table(m_pSqli3db, szSql, &pazResult, &nRow, &nCol, &psqlite_error);
  129. if ( sqlite_error != SQLITE_OK)
  130. {
  131. #ifdef _UNICODE
  132. WCHAR wszError[512] = {0};
  133. Char2WChar(wszError, psqlite_error);
  134. strError = wszError;
  135. #else
  136. strError = psqlite_error;
  137. #endif
  138. return -1;
  139. }
  140. for(int i=1; i<=nRow; i++)
  141. {
  142. for(int j = 0; j<nCol; j++)
  143. {
  144. #ifdef _UNICODE
  145. WCHAR wszValue[256] = {0};
  146. ascii2unicode(pazResult[i*nCol + j], wszValue);
  147. vValues.push_back(wszValue);
  148. #else
  149. vValues.push_back(pazResult[i*nCol + j]);
  150. #endif
  151. }
  152. }
  153. sqlite3_free_table(pazResult);
  154. return 0;
  155. }
  156. /************************************************************************/
  157. /*
  158. 函数: Insert
  159. 描述: 添加数据到表
  160. 参数:
  161. const char* pTableName, 表名
  162. const char* pFields, 字段集
  163. const char* pValues, 要插的数据集
  164. CString& strError 返回错误消息
  165. 返回:
  166. 注意: 0成功, -1失败;
  167. */
  168. /************************************************************************/
  169. int CSQLiter3Interface::Insert(const char* pTableName, const char* pFields, const char* pValues, CString& strError)
  170. {
  171. if(m_pSqli3db == NULL || pTableName == NULL || pFields == NULL || pValues == NULL)
  172. return -1;
  173. char szSql[MAX_PATH] = {0};
  174. sprintf(szSql, "insert into %s(%s) values (%s);", pTableName, pFields, pValues);
  175. return Exec(szSql, strError);
  176. }
  177. /************************************************************************/
  178. /*
  179. 函数: Delete
  180. 描述: 删除 delete from table_name where field=''
  181. 参数:
  182. const char* pTableName, 表名
  183. const char* pFields, 字段集
  184. const char* pValues, 要插的数据集
  185. CString& strError 返回错误消息
  186. 返回:
  187. 注意: 0成功, -1失败;
  188. */
  189. /************************************************************************/
  190. int CSQLiter3Interface::Delete(const char* pTableName, const char* pLimit, CString& strError)
  191. {
  192. if(m_pSqli3db == NULL || pTableName == NULL || pLimit == NULL)
  193. return -1;
  194. char szSql[MAX_PATH] = {0};
  195. sprintf(szSql, "delete from %s where %s;", pTableName, pLimit);
  196. return Exec(szSql, strError);
  197. }
  198. /************************************************************************/
  199. /*
  200. 函数: Colse
  201. 描述: 关闭
  202. 参数:
  203. 返回:
  204. 注意: 0成功, -1失败;
  205. */
  206. /************************************************************************/
  207. int CSQLiter3Interface::Close()
  208. {
  209. if(m_pSqli3db)
  210. sqlite3_close(m_pSqli3db);
  211. m_pSqli3db = NULL;
  212. return 0;
  213. }