C语言操作sqlite3对表的插入和查询

 
 
  1. #ifndef__MYDB_H
  2. #define__MYDB_H
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include"sqlite3.h"
  6. #defineDB_NAME"test.db"
  7. #defineTABLE_NAME"student"
  8. #defineTRUE1
  9. #defineFALSE0
  10. intopen_db();
  11. intmodefiy_data();
  12. intinsert_data();
  13. intsearch_data();
  14. #endif
 
 
  1. #include"mydb.h"
  2. sqlite3*pdb=NULL;
  3. char*szErrMsg=0;
  4. /*
  5. *@brief//打开数据库
  6. *@parampdb类型为sqlite
  7. */
  8. intopen_db()
  9. {
  10. intrc;
  11. rc=sqlite3_open(DB_NAME,&pdb);
  12. if(rc)
  13. {
  14. fprintf(stderr,"can'topendatabase:%s\n",sqlite3_errmsg(pdb));
  15. sqlite3_close(pdb);
  16. return-1;
  17. }
  18. return0;
  19. }
  20. /*
  21. //@brif数据库语句执行
  22. intcreat_table()
  23. {
  24. intrc;
  25. char*strsql="createtablestudent(sidvarchar(10)notnullprimarykey,snamevarchar(10)notnull,timeDatetime)";
  26. sqlite3_exec(pdb,strsql,&szErrMsg);
  27. if(rc!=0)
  28. {
  29. fprintf(stderr,"can'topendatabase:%s\n",sqlite3_errmsg(pdb));
  30. sqlite3_close(pdb);
  31. return-1;
  32. }
  33. return0;
  34. }
  35. */
  36. intmodefiy_data()
  37. {
  38. if(open_db!=0)
  39. {
  40. open_db();
  41. }
  42. return0;
  43. }
  44. /*
  45. *数据的添加
  46. */
  47. intinsert_data()
  48. {
  49. intflag=TRUE;
  50. charbuff[1024];
  51. char*strsql;
  52. intrc=0;
  53. chartemp[3][20];
  54. strsql=buff;
  55. if(open_db!=0)
  56. {
  57. open_db();
  58. }
  59. strcpy(strsql,"insertinto");
  60. strcat(strsql,TABLE_NAME);
  61. strcat(strsql,"values(?,?,?);");
  62. sqlite3_stmt*stmt=NULL;
  63. rc=sqlite3_prepare_v2(pdb,strlen(strsql),&stmt,NULL);
  64. if(rc!=sqlITE_OK)
  65. {
  66. if(stmt)
  67. {
  68. sqlite3_finalize(stmt);
  69. }
  70. sqlite3_close(pdb);
  71. return-1;
  72. }
  73. while(flag)
  74. {
  75. getchar();
  76. memset(temp,sizeof(temp));
  77. printf("学号:");
  78. scanf("%s",temp[0]);
  79. printf("姓名:");
  80. scanf("%s",temp[1]);
  81. printf("性别:");
  82. scanf("%s",temp[2]);
  83. //getchar();
  84. sqlite3_bind_text(stmt,1,temp[0],strlen(temp[0]),NULL);
  85. sqlite3_bind_text(stmt,2,temp[1],strlen(temp[1]),3,temp[2],strlen(temp[2]),NULL);
  86. if(sqlite3_step(stmt)!=sqlITE_DONE)
  87. {
  88. sqlite3_finalize(stmt);
  89. sqlite3_close(pdb);
  90. printf("faile\n");
  91. return0;
  92. }
  93. sqlite3_reset(stmt);
  94. printf("Doyouwanttodoinsert?[0~1]:");
  95. scanf("%d",&flag);
  96. }
  97. sqlite3_finalize(stmt);
  98. printf("insertsuccess!\n");
  99. sqlite3_close(pdb);
  100. return0;
  101. }
  102. /*
  103. *数据的查询
  104. */
  105. intsearch_data()
  106. {
  107. intrc;
  108. if(open_db!=0)
  109. {
  110. open_db();
  111. }
  112. char*strsql="select*fromstudent";
  113. sqlite3_stmt*stmt=NULL;
  114. rc=sqlite3_prepare_v2(pdb,NULL);
  115. if(rc!=sqlITE_OK)
  116. {
  117. if(stmt)
  118. {
  119. sqlite3_finalize(stmt);
  120. }
  121. sqlite3_close(pdb);
  122. return-1;
  123. }
  124. intnColumn=sqlite3_column_count(stmt);
  125. intvtype,i;
  126. do{
  127. rc=sqlite3_step(stmt);
  128. if(rc==sqlITE_ROW)
  129. {
  130. for(i=0;i<nColumn;i++)
  131. {
  132. vtype=sqlite3_column_type(stmt,i);
  133. if(vtype==sqlITE_INTEGER)
  134. {
  135. printf("%s:%d\n",sqlite3_column_name(stmt,i),sqlite3_column_int(stmt,i));
  136. }
  137. elseif(vtype==sqlITE_TEXT)
  138. {
  139. printf("%s:%s\n",sqlite3_column_text(stmt,i));
  140. }
  141. elseif(vtype==sqlITE_NULL)
  142. {
  143. printf("novalues\n");
  144. }
  145. }
  146. printf("\n****************\n");
  147. }
  148. elseif(rc==sqlITE_DONE)
  149. {
  150. printf("Selectfinish\n");
  151. break;
  152. }
  153. else
  154. {
  155. printf("Selectfaile\n");
  156. sqlite3_finalize(stmt);
  157. break;
  158. }
  159. }while(1);
  160. sqlite3_finalize(stmt);
  161. sqlite3_close(pdb);
  162. return0;
  163. }
 
 
  1. #include<stdio.h>
  2. #include"mydb.h"
  3. intmain(intargc,char**argv)
  4. {
  5. intn;
  6. printf("\n1.添加2.修改3.删除4.查看\n");
  7. printf("choose[1-4]:");
  8. scanf("%d",&n);
  9. switch(n)
  10. {
  11. case1:
  12. insert_data();
  13. //printf("添加成功\n");
  14. break;
  15. case2:
  16. printf("修改成功\n");
  17. break;
  18. case3:
  19. printf("删除成功\n");
  20. break;
  21. case4:
  22. search_data();
  23. printf("查看成功\n");
  24. break;
  25. default:
  26. printf("\nerror\n");
  27. }
  28. return0;
  29. }

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能