• 作业 增删改查 三个功能


    1. ubuntu@ubuntu:~/wlbc/day7$ cat sqlite01.c
    2. #include
    3. #include
    4. #include
    5. #include
    6. int increas(sqlite3 *db,char *errmsg)
    7. {
    8. char sql[128]="";
    9. //printf("请输入要增加的库\n");
    10. //scanf("%s",unfold);
    11. char aa[128]="INSERT INTO stu VALUES";
    12. char id[128]="";
    13. char name[128]="";
    14. char scoer[128]="";
    15. printf("输入要增加的数据 ID,NAME,SCORE\n");
    16. scanf("%s %s %s",id,name,scoer);
    17. while(getchar()!=10);
    18. sprintf(sql,"%s(%s,'%s',%s)",aa,id,name,scoer);
    19. printf("%s\n",sql);
    20. //sprintf()
    21. if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    22. {
    23. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    24. return -1;
    25. }
    26. }
    27. int del_sqlite(sqlite3* db,char* errmsg)
    28. {
    29. char id[128]="";
    30. char name[128]="";
    31. char scoer[128]="";
    32. printf("请输入你要删除你的数据信息\n");
    33. scanf("%s %s %s",id,name,scoer);
    34. while(getchar()!=10);
    35. char qq[128]="delete from stu where id";
    36. char sql[128]="";
    37. sprintf(sql,"%s=%s",qq,id);
    38. if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    39. {
    40. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    41. return-1;
    42. }
    43. }
    44. int revise_sql(sqlite3* db,char* errmsg)
    45. {
    46. char id[128]="";
    47. char name[128]="";
    48. char scoer[128]="";
    49. printf("请输入你要修改的数据的信息\n");
    50. scanf("%s %s %s",id,name,scoer);
    51. while(getchar()!=10);
    52. char name1[128]="";
    53. char scoer1[128]="";
    54. printf("id 不能修改\n");
    55. printf("如果要修改name,请输入要修改的name如果不是请输入NO\n");
    56. scanf("%s",name1);
    57. while(getchar()!=10);
    58. printf("如果要修改scoer,请输入要修改的scoer如果不是请输入NO\n");
    59. scanf("%s",scoer1);
    60. while(getchar()!=10);
    61. char sql[128]="";
    62. char sql2[128]="";
    63. char qq[128]="update stu set";
    64. sprintf(sql,"%s name='%s'where id=%s",qq,name1,id);
    65. sprintf(sql2,"%s score=%s where id=%s",qq,scoer1,id);
    66. if(strcmp(name1,"NO")!=0)
    67. {
    68. if(strcmp(scoer1,"NO")!=0)
    69. {
    70. if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    71. {
    72. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    73. return -1;
    74. }
    75. if(sqlite3_exec(db,sql2,NULL,NULL,&errmsg)!=SQLITE_OK)
    76. {
    77. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    78. return -1;
    79. }
    80. }
    81. else
    82. {
    83. if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    84. {
    85. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    86. return -1;
    87. }
    88. }
    89. }
    90. if(strcmp(scoer1,"NO")!=0)
    91. {
    92. if(sqlite3_exec(db,sql2,NULL,NULL,&errmsg)!=SQLITE_OK)
    93. {
    94. fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
    95. return -1;
    96. }
    97. }
    98. }
    99. int main(int argc, const char *argv[])
    100. {
    101. sqlite3 *db =NULL;
    102. if(sqlite3_open("./my.db",&db)!= SQLITE_OK)
    103. {
    104. fprintf(stderr,"line:%d sqlite3_open:%s\n",__LINE__,sqlite3_errmsg(db));
    105. fprintf(stderr,"line:%d sqlite3_open:%d\n",__LINE__,sqlite3_errcode(db));
    106. return-1;
    107. }
    108. char sql[128]="create table if not exists stu (id int,name char,score float);";
    109. char*errmsg = NULL;
    110. if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
    111. {
    112. fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
    113. return -1;
    114. }
    115. printf("创建数据库成功\n");
    116. char c =0;
    117. while(1)
    118. {
    119. system("clear");//清屏
    120. printf("------------------------------\n");
    121. printf("---------1.添加---------------\n");
    122. printf("---------2.删除---------------\n");
    123. printf("---------3.改-----------------\n");
    124. printf("------------------------------\n");
    125. printf("----------5.退出--------------\n");
    126. printf("请输入\n");
    127. c=getchar();
    128. while(getchar()!=10);
    129. switch(c)
    130. {
    131. case '1':
    132. increas(db,errmsg);
    133. break;
    134. case '2':
    135. del_sqlite(db,errmsg);
    136. break;
    137. case '3':
    138. revise_sql(db,errmsg);
    139. break;
    140. case'5':
    141. goto END;
    142. default:
    143. printf("输入错误,重新输入\n");
    144. }
    145. printf("输入任何字符清零\n");
    146. while(getchar()!=10);
    147. }
    148. END:
    149. if(sqlite3_close(db)!=SQLITE_OK)
    150. {
    151. fprintf(stderr,"line:%d sqlite3_open: %s\n",__LINE__,sqlite3_errmsg(db));
    152. fprintf(stderr,"line:%d sqlite3_open: %d\n",__LINE__,sqlite3_errcode(db));
    153. }
    154. return 0;
    155. }

  • 相关阅读:
    vue-h5移动Web的rem配置
    【数据结构】顺序表详解
    Excel中文本数字单元格批量转换(不影响公式单元格)
    Elasticsearch head 连接 阿里云ES
    浅析目标检测入门算法:YOLOv1,SSD,YOLOv2,YOLOv3,CenterNet,EfficientDet,YOLOv4
    论文阅读(3) 用气泡PIV测量加利福尼亚海狮推进冲程的速度场
    Optisystem应用:光电检测器灵敏度建模
    可变参数函数,initializer_list,省略号形参
    tensorflow深度学习模型读取parquet数据进行训练实现
    初识html
  • 原文地址:https://blog.csdn.net/shen20010928/article/details/128088341