testsqlite3.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "DataImpl.h"
  13. #include "TaskInfoMessage.pb.h"
  14. // 唯一的应用程序对象
  15. CWinApp theApp;
  16. using namespace std;
  17. static int callback(void *NotUsed, int argc, char **argv, char **azColName)
  18. {
  19. int i;
  20. for(i=0; i<argc; i++)
  21. {
  22. printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  23. }
  24. printf("\n");
  25. return 0;
  26. }
  27. #define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK) \
  28. {printf("%s error!\n",szInfo);\
  29. printf("%s\n",szErrMsg); \
  30. sqlite3_free(szErrMsg); \
  31. sqlite3_close(db); \
  32. system("pause");\
  33. return 0;}
  34. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  35. {
  36. int nRetCode = 0;
  37. // 初始化 MFC 并在失败时显示错误
  38. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  39. {
  40. // TODO: 更改错误代码以符合您的需要
  41. _tprintf(_T("错误: MFC 初始化失败\n"));
  42. nRetCode = 1;
  43. }
  44. else
  45. {
  46. #if 0
  47. // TODO: 在此处为应用程序的行为编写代码。
  48. sqlite3 *db;
  49. char *dbPath="F:\\输出文件\\服务端\\test.db";
  50. char *szErrMsg = 0;
  51. char szpath[MAX_PATH] = {0};
  52. std::string ss= "F:\\输出文件\\服务端\\test.db";
  53. if ( StringProcess::ascii_to_utf8(dbPath, szpath) == 0)
  54. return 0;
  55. StringProcess::GB2312ToUTF_8(ss, dbPath, strlen(dbPath));
  56. sprintf(szpath, _T("%s"), ss.c_str());
  57. if ( -1 == _access(dbPath, 0 ) )
  58. {
  59. int rc= sqlite3_open(szpath, &db);
  60. CHECK_RC(rc,"open database",szErrMsg,db);
  61. char *szSql="create table UserInfo(ID int primary key , UserName char, PassWord char);";
  62. rc=sqlite3_exec(db,szSql,0,0,&szErrMsg);
  63. CHECK_RC(rc,"create table",szErrMsg,db);
  64. rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(1,'kfqcome','123456')",0,0,&szErrMsg);
  65. CHECK_RC(rc,"insert info",szErrMsg,db);
  66. rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(2,'miss wang','654321')",0,0,&szErrMsg);
  67. CHECK_RC(rc,"insert info",szErrMsg,db);
  68. szSql="select * from UserInfo";
  69. rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
  70. CHECK_RC(rc,"query values",szErrMsg,db);
  71. sqlite3_close(db);
  72. }
  73. else
  74. {
  75. int rc= sqlite3_open(szpath, &db);
  76. CHECK_RC(rc,"open database",szErrMsg,db);
  77. char *szSql="select * from UserInfo";
  78. rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
  79. CHECK_RC(rc,"query values",szErrMsg,db);
  80. sqlite3_close(db);
  81. }
  82. #endif
  83. #if 0
  84. CHAR *pszError = NULL;
  85. sqlite3 *pObj = NULL;
  86. // 打开;
  87. INT nResult = sqlite3_open("sms.db", &pObj);
  88. if ( SQLITE_OK != nResult )
  89. return -1;
  90. // 创建;
  91. #define _CREATE_TABLE_ "CREATE TABLE UserInfo (Autoid INTEGER NOT NULL,Account TEXT(16) NOT NULL,Password TEXT(16) NOT NULL,PRIMARY KEY (Autoid ASC),CONSTRAINT IU_UserInfo UNIQUE (Account));"
  92. //nResult = sqlite3_exec(pObj, _CREATE_TABLE_, NULL, NULL, &pszError);
  93. if ( SQLITE_OK != nResult )
  94. {
  95. printf("%s\n", pszError);
  96. }
  97. // 插入;
  98. CStringA strInsert = "";
  99. nResult = sqlite3_exec(pObj, "insert into UserInfo(Autoid, Account, Password) values (2, 'aaaa', '123456')", NULL, NULL, &pszError);
  100. if ( SQLITE_OK != nResult )
  101. {
  102. printf("%s\n", pszError);
  103. }
  104. // 查询;
  105. INT nRow = 0;
  106. INT nCol = 0;
  107. CHAR **pazResult = NULL;
  108. nResult = sqlite3_get_table(pObj, "select * from UserInfo", &pazResult, &nRow, &nCol, &pszError);
  109. if ( SQLITE_OK != nResult )
  110. {
  111. printf("%s\n", pszError);
  112. }
  113. else
  114. {
  115. // 解析表数据;
  116. std::vector<string> vValues;
  117. for ( int i = 1; i <= nRow; i++ )
  118. {
  119. for(int j = 0; j<nCol; j++)
  120. {
  121. #ifdef _UNICODE
  122. WCHAR wszValue[256] = {0};
  123. StringProcess::ascii_to_unicode(pazResult[i*nCol + j], wszValue);
  124. vValues.push_back(wszValue);
  125. #else
  126. vValues.push_back(pazResult[i*nCol + j]);
  127. #endif
  128. }
  129. }
  130. // 释放;
  131. sqlite3_free_table(pazResult);
  132. }
  133. #endif
  134. #if 1
  135. if (CDataImpl::GetInstance()->Open())
  136. {
  137. if (0)
  138. {// 插入数据;
  139. satdDatabase::complieServer tagServer;
  140. tagServer.set_server("192.168.1.1");
  141. tagServer.set_user("home");
  142. tagServer.set_password("password99999");
  143. CDataImpl::GetInstance()->InsertComplieServer(tagServer);
  144. // 查询数据;
  145. satdDatabase::listServer list;
  146. int nRow = CDataImpl::GetInstance()->QueryComplieServer(list);
  147. for (int i = 0; i < list.servers_size(); i++)
  148. {
  149. satdDatabase::complieServer ser = list.servers(i);
  150. OutputDebugString(ser.server().c_str()); OutputDebugString("\n");
  151. OutputDebugString(ser.user().c_str()); OutputDebugString("\n");
  152. OutputDebugString(ser.password().c_str()); OutputDebugString("\n");
  153. }
  154. }
  155. if (0)
  156. {
  157. satdDatabase::model tagModel;
  158. tagModel.set_name("set_name");
  159. tagModel.set_softsuffix("set_softsuffix");
  160. satdDatabase::listEndFlag* pflags = tagModel.mutable_flags();
  161. pflags->add_terminator("end1");
  162. pflags->add_terminator("end2");
  163. pflags->add_terminator("end3");
  164. pflags->add_terminator("end4");
  165. pflags->add_terminator("end5");
  166. satdDatabase::modelContent *pMc = tagModel.mutable_content();
  167. pMc->set_complietime("set_complietime");
  168. pMc->set_projectmodel("set_projectmodel");
  169. pMc->set_productname("set_productname");
  170. pMc->set_svndir("set_svndir");
  171. pMc->set_command("set_command");
  172. pMc->set_projectname("set_projectname");
  173. pMc->set_projectsn("set_projectsn");
  174. pMc->set_softver("set_softver");
  175. pMc->set_svnver("set_svnver");
  176. pMc->set_projectid("set_projectid");
  177. pMc->set_zone("set_zone");
  178. pMc->set_uistyle("set_uistyle");
  179. pMc->set_compliefiledir("set_compliefiledir");
  180. pMc->set_username("set_username");
  181. pMc->set_time("set_time");
  182. CDataImpl::GetInstance()->InsertAutoComplieModel(tagModel);
  183. // 查询;
  184. satdDatabase::listModel models;
  185. CDataImpl::GetInstance()->QueryAutoComplieModel(models);
  186. for ( int i = 0; i < models.models_size(); i++ )
  187. {
  188. satdDatabase::model md = models.models(i);
  189. OutputDebugString(md.name().c_str()); OutputDebugString("\n");
  190. OutputDebugString(md.softsuffix().c_str()); OutputDebugString("\n");
  191. pflags = md.mutable_flags();
  192. for ( int j = 0; j < md.mutable_flags()->terminator_size(); j++)
  193. {
  194. OutputDebugString(pflags->terminator(j).c_str()); OutputDebugString("\n");
  195. }
  196. pMc = md.mutable_content();
  197. OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
  198. OutputDebugString(pMc->projectmodel().c_str()); OutputDebugString("\n");
  199. OutputDebugString(pMc->productname().c_str()); OutputDebugString("\n");
  200. OutputDebugString(pMc->svndir().c_str()); OutputDebugString("\n");
  201. OutputDebugString(pMc->command().c_str()); OutputDebugString("\n");
  202. OutputDebugString(pMc->projectname().c_str()); OutputDebugString("\n");
  203. OutputDebugString(pMc->projectsn().c_str()); OutputDebugString("\n");
  204. OutputDebugString(pMc->softver().c_str()); OutputDebugString("\n");
  205. OutputDebugString(pMc->projectid().c_str()); OutputDebugString("\n");
  206. OutputDebugString(pMc->zone().c_str()); OutputDebugString("\n");
  207. OutputDebugString(pMc->uistyle().c_str()); OutputDebugString("\n");
  208. OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
  209. OutputDebugString(pMc->username().c_str()); OutputDebugString("\n");
  210. OutputDebugString(pMc->time().c_str()); OutputDebugString("\n");
  211. }
  212. }
  213. if (0)
  214. {
  215. satdDatabase::model tagModel;
  216. tagModel.set_name("set_name");
  217. tagModel.set_softsuffix("set_softsuffix__dddddd");
  218. satdDatabase::listEndFlag* pflags = tagModel.mutable_flags();
  219. pflags->add_terminator("end111");
  220. pflags->add_terminator("end222");
  221. pflags->add_terminator("end333");
  222. pflags->add_terminator("end444");
  223. pflags->add_terminator("end555");
  224. satdDatabase::modelContent *pMc = tagModel.mutable_content();
  225. pMc->set_complietime("set_complietimedddd");
  226. pMc->set_projectmodel("set_projectmodelddd");
  227. pMc->set_productname("set_productnameddd");
  228. pMc->set_svndir("set_svndidddr");
  229. pMc->set_command("set_commadddnd");
  230. pMc->set_projectname("set_projectnameddd");
  231. pMc->set_projectsn("set_projectsnddd");
  232. pMc->set_softver("set_softverddd");
  233. pMc->set_svnver("set_svnverddd");
  234. pMc->set_projectid("set_projectidddd");
  235. pMc->set_zone("set_zoneddd");
  236. pMc->set_uistyle("set_uistyleddd");
  237. pMc->set_compliefiledir("set_compliefiledirddd");
  238. pMc->set_username("set_username");
  239. pMc->set_time("set_time");
  240. CDataImpl::GetInstance()->UpdateAutoCompliteModel(tagModel);
  241. // 查询;
  242. satdDatabase::listModel models;
  243. CDataImpl::GetInstance()->QueryAutoComplieModel(models);
  244. for (int i = 0; i < models.models_size(); i++)
  245. {
  246. satdDatabase::model md = models.models(i);
  247. OutputDebugString(md.name().c_str()); OutputDebugString("\n");
  248. OutputDebugString(md.softsuffix().c_str()); OutputDebugString("\n");
  249. pflags = md.mutable_flags();
  250. for (int j = 0; j < md.mutable_flags()->terminator_size(); j++)
  251. {
  252. OutputDebugString(pflags->terminator(j).c_str()); OutputDebugString("\n");
  253. }
  254. pMc = md.mutable_content();
  255. OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
  256. OutputDebugString(pMc->projectmodel().c_str()); OutputDebugString("\n");
  257. OutputDebugString(pMc->productname().c_str()); OutputDebugString("\n");
  258. OutputDebugString(pMc->svndir().c_str()); OutputDebugString("\n");
  259. OutputDebugString(pMc->command().c_str()); OutputDebugString("\n");
  260. OutputDebugString(pMc->projectname().c_str()); OutputDebugString("\n");
  261. OutputDebugString(pMc->projectsn().c_str()); OutputDebugString("\n");
  262. OutputDebugString(pMc->softver().c_str()); OutputDebugString("\n");
  263. OutputDebugString(pMc->projectid().c_str()); OutputDebugString("\n");
  264. OutputDebugString(pMc->zone().c_str()); OutputDebugString("\n");
  265. OutputDebugString(pMc->uistyle().c_str()); OutputDebugString("\n");
  266. OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
  267. OutputDebugString(pMc->username().c_str()); OutputDebugString("\n");
  268. OutputDebugString(pMc->time().c_str()); OutputDebugString("\n");
  269. }
  270. }
  271. if (1)
  272. {
  273. CDataImpl::GetInstance()->ExecteSQL("delete from model where name = 'set_name'");
  274. }
  275. }
  276. #endif
  277. //getchar();
  278. system("pause");
  279. }
  280. return nRetCode;
  281. }