加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 20:25:48 所属栏目:百科 来源:网络整理
导读:#ifndef__MYDB_H #define__MYDB_H #includestdio.h #includestring.h #include"sqlite3.h" #defineDB_NAME"test.db" #defineTABLE_NAME"student" #defineTRUE1 #defineFALSE0 int open_db(); int modefiy_data(); int insert_data(); int search_data(); #en
 
 
  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:%sn",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:%sn",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("failen");
  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:%dn",sqlite3_column_name(stmt,i),sqlite3_column_int(stmt,i));
  136. }
  137. elseif(vtype==SQLITE_TEXT)
  138. {
  139. printf("%s:%sn",sqlite3_column_text(stmt,i));
  140. }
  141. elseif(vtype==SQLITE_NULL)
  142. {
  143. printf("novaluesn");
  144. }
  145. }
  146. printf("n****************n");
  147. }
  148. elseif(rc==SQLITE_DONE)
  149. {
  150. printf("Selectfinishn");
  151. break;
  152. }
  153. else
  154. {
  155. printf("Selectfailen");
  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("nerrorn");
  27. }
  28. return0;
  29. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读