testsqlite3.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // testsqlite3.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "testsqlite3.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. #include <io.h>
  9. #include "..\\SQLite3\\sqlite3.h"
  10. #pragma comment(lib, "SQLite3.lib")
  11. #include "StringProcess.h"
  12. // 唯一的应用程序对象
  13. CWinApp theApp;
  14. using namespace std;
  15. #define __USE_KEY__
  16. static int callback(void *NotUsed, int argc, char **argv, char **azColName)
  17. {
  18. int i;
  19. for(i=0; i<argc; i++)
  20. {
  21. printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  22. }
  23. printf("\n");
  24. return 0;
  25. }
  26. #define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK) \
  27. {printf("%s error!\n",szInfo);\
  28. printf("%s\n",szErrMsg); \
  29. sqlite3_free(szErrMsg); \
  30. sqlite3_close(db); \
  31. system("pause");\
  32. return 0;}
  33. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  34. {
  35. int nRetCode = 0;
  36. // 初始化 MFC 并在失败时显示错误
  37. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  38. {
  39. // TODO: 更改错误代码以符合您的需要
  40. _tprintf(_T("错误: MFC 初始化失败\n"));
  41. nRetCode = 1;
  42. }
  43. else
  44. {
  45. // TODO: 在此处为应用程序的行为编写代码。
  46. sqlite3 *db;
  47. char *dbPath="test.db";
  48. char *szErrMsg = 0;
  49. char szpath[MAX_PATH] = {0};
  50. std::string ss= "test.db";
  51. if ( StringProcess::ascii_to_utf8(dbPath, szpath) == 0)
  52. return 0;
  53. StringProcess::GB2312ToUTF_8(ss, dbPath, strlen(dbPath));
  54. char *szSql = NULL;
  55. sprintf(szpath, _T("%s"), ss.c_str());
  56. if ( -1 == _access(dbPath, 0 ) )
  57. {
  58. int rc= sqlite3_open(szpath, &db);
  59. #ifdef __USE_KEY__
  60. if ( rc == SQLITE_OK )
  61. {
  62. // rekye,对未加密的数据加密;
  63. //if ( SQLITE_OK != sqlite3_rekey(db, "0123456789abcdef", 16))
  64. if ( SQLITE_OK != sqlite3_key(db, "0123456789abcdef", 16))
  65. {
  66. //szErrMsg = "sqlite encrypt error:";
  67. CHECK_RC(rc,"sqlite encrypt error:",szErrMsg,db);
  68. }
  69. }
  70. #endif
  71. rc=sqlite3_exec(db,"PRAGMA synchronous = OFF",0,0,0); //提高性能
  72. rc=sqlite3_exec(db,"PRAGMA cache_size = 8000",0,0,0); //加大缓存
  73. rc=sqlite3_exec(db,"PRAGMA count_changes = 1",0,0,0); //返回改变记录数
  74. rc=sqlite3_exec(db,"PRAGMA case_sensitive_like = 1",0,0,0); //支持中文LIKE查询
  75. CHECK_RC(rc,"open database",szErrMsg,db);
  76. szSql="create table UserInfo(ID int primary key , UserName char, PassWord char);";
  77. rc=sqlite3_exec(db,szSql,0,0,&szErrMsg);
  78. CHECK_RC(rc,"create table",szErrMsg,db);
  79. rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(1,'kfqcome','123456')",0,0,&szErrMsg);
  80. CHECK_RC(rc,"insert info",szErrMsg,db);
  81. rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(2,'miss wang','654321')",0,0,&szErrMsg);
  82. CHECK_RC(rc,"insert info",szErrMsg,db);
  83. szSql="select * from UserInfo";
  84. rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
  85. CHECK_RC(rc,"query values",szErrMsg,db);
  86. sqlite3_close(db);
  87. }
  88. else
  89. {
  90. int rc= sqlite3_open(szpath, &db);
  91. CHECK_RC(rc,"open database",szErrMsg,db);
  92. #ifdef __USE_KEY__
  93. if ( SQLITE_OK != sqlite3_key(db, "0123456789abcdef", 16))
  94. {
  95. //szErrMsg = "sqlite encrypt error:";
  96. CHECK_RC(rc,"sqlite encrypt error:",szErrMsg,db);
  97. }
  98. #endif
  99. char *szSql="select * from UserInfo";
  100. rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
  101. CHECK_RC(rc,"query values",szErrMsg,db);
  102. sqlite3_close(db);
  103. }
  104. //getchar();
  105. system("pause");
  106. }
  107. return nRetCode;
  108. }