ISQLite3.cpp 5.8 KB

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