当前位置:   article > 正文

网络编程模拟面试题总结, sqlite3的c语言调用,

网络编程模拟面试题总结, sqlite3的c语言调用,

1 数据库的插入删除改变和查看

  1. int do_insert(sqlite3* db);
  2. int do_delet(sqlite3* db);
  3. int do_select(sqlite3*db);
  4. int do_change(sqlite3* db);
  5. /*sqlite3* db=NULL;*/
  6. int main(int argc, const char *argv[])
  7. {
  8. sqlite3* db=NULL;
  9. if(sqlite3_open("./my.db",&db)){
  10. printf("%s\n",sqlite3_errmsg(db));
  11. return -1;
  12. }
  13. printf("sqlite3_open success\n");
  14. char str[128]="create table if not exists stu (id int key,name char,score int)";
  15. char*errmsg=NULL;
  16. if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
  17. fprintf(stderr,"create failed __%d__\n",__LINE__);
  18. return -1;
  19. }
  20. char c=0;
  21. while(1){
  22. system("clear");
  23. printf("------------------\n");
  24. printf("------------------\n");
  25. printf("------1.insert----\n");
  26. printf("------2.delet-----\n");
  27. printf("------3.change----\n");
  28. printf("------4.select----\n");
  29. printf("------5.quit------\n");
  30. printf("------------------\n");
  31. c=getchar();
  32. while(getchar()!='\n');
  33. switch (c){
  34. case'1':
  35. do_insert(db);
  36. break;
  37. case'2':
  38. do_delet(db);
  39. break;
  40. case'3':
  41. do_change(db);
  42. break;
  43. case'4':
  44. do_select(db);
  45. break;
  46. case'5':
  47. goto END;
  48. }
  49. fprintf(stderr,"input space clear>>");
  50. while(getchar()!='\n');
  51. }
  52. END:
  53. sqlite3_close(db);
  54. return 0;
  55. }
  56. int do_insert(sqlite3* db){
  57. int id=0;
  58. char name[20]="";
  59. int score=0;
  60. printf("please input id>>");
  61. scanf("%d",&id);
  62. while(getchar()!='\n');
  63. printf("please input name>>");
  64. scanf("%s",name);
  65. while(getchar()!='\n');
  66. printf("please input score>>");
  67. scanf("%d",&score);
  68. while(getchar()!='\n');
  69. char str[128]="";
  70. sprintf(str,"INSERT INTO stu values(%d, \"%s\",%d);",id,name,score);
  71. char*errmsg=NULL;
  72. if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
  73. fprintf(stderr,"insert failed __%d__\n",__LINE__);
  74. return -1;
  75. }
  76. fprintf(stderr,"insert in to stu success\n");
  77. printf("id=%d,name=%s,score=%d\n",id,name,score);
  78. return 0;
  79. }
  80. int do_delet(sqlite3* db){
  81. do_select(db);
  82. int id=0;
  83. printf("please input the id who needing be deleted>>");
  84. scanf("%d",&id);
  85. while(getchar()!='\n');
  86. char str[128]="";
  87. sprintf(str,"delet from stu where id=%d;",id);
  88. char*errmsg=NULL;
  89. if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
  90. fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
  91. return -1;
  92. }
  93. fprintf(stderr,"delet id=%d from stu success\n",id);
  94. return 0;
  95. }
  96. int do_change(sqlite3* db){
  97. again:
  98. do_select(db);
  99. int id=0;
  100. char name[20]="";
  101. int score=0;
  102. printf("please input the id who needing be changed>>");
  103. scanf("%d",&id);
  104. while(getchar()!='\n');
  105. printf("please input the member from stu who need change:");
  106. printf("1.name 2.score\n");
  107. char ctrl=getchar();
  108. if('1'==ctrl){
  109. printf("please input the new name:");
  110. scanf("%s",name);
  111. char str[128]="";
  112. sprintf(str,"update stu set name=%s where id=%d;",name,id);
  113. char*errmsg=NULL;
  114. if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
  115. fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
  116. return -1;
  117. }
  118. fprintf(stderr,"delet id=%d from stu success\n",id);
  119. }else if('2'==ctrl){
  120. printf("please input the new score:");
  121. scanf("%d",&score);
  122. char str[128]="";
  123. sprintf(str,"update stu set score=%d where id=%d;",score,id);
  124. char*errmsg=NULL;
  125. if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
  126. fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
  127. return -1;
  128. }
  129. fprintf(stderr,"delet id=%d from stu success\n",id);
  130. }else{
  131. printf("input error!\n");
  132. printf("please input again");
  133. while(getchar()!='\n');
  134. goto again;
  135. }
  136. printf("change success\n");
  137. return 0;
  138. }
  139. int do_select(sqlite3*db){
  140. char str[128]={0};
  141. sprintf(str,"select * from stu;");
  142. char* errmsg=NULL;
  143. char**retPtr=NULL;
  144. int row;
  145. int column;
  146. if(sqlite3_get_table(db,str,&retPtr,&row,&column,&errmsg)!=SQLITE_OK){
  147. fprintf(stderr,"get_table failed __%d__,errno=%s\n",__LINE__,errmsg);
  148. return -1;
  149. }
  150. for(int i=0;i<row;i++){
  151. for(int j=0;j<column;j++){
  152. fprintf(stderr,"%-8s\t",*(retPtr+(i*column)+j));
  153. }
  154. putchar(10);
  155. }
  156. printf("select success\n");
  157. }

2 模拟面试试题

(1)IO多路复用的原理:将需要监控的文件描述符放到一个容器内统一监管,一旦容器中的文件描述符有一个或多个对应的事件产生,就会将阻塞解除,去执行接下来的代码。

(2)select和poll的区别:select是将fdset拷贝到内核,通过循环遍历每个fd文件描述符来监控事件,所以上述缺陷倒置select最多只能监控1024个文件描述符,而poll没有这个限制。

(3)数据库sqlite3函数有哪些:sqlite3_open sqlite3_close sqlite3_exec sqlite3_get_table

sqlite3_free_table

sqlite3_exec的第三个参数是一个回调函数,每当exec所执行的语句有一个结果就会调用一次该回调函数。

(4)什么是IO:一个程序与外部设备间的信息交互

(5)文件IO的函数谁提供?标准IO的函数谁提供?:系统内核提供,c语言库提供

(6)消息队列和共享内存的区别:消息队列内的信息在被读取之前一直存在,不易丢失,共享内存只要有一方改变其中内容,其内容就会被实时更新,实时性好

(7)野指针产生情况:未初始化的指针,释放后未指向NULL的指针,访问数组越界的指针,指向非法内容的指针。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号