赞
踩
- int do_insert(sqlite3* db);
- int do_delet(sqlite3* db);
- int do_select(sqlite3*db);
- int do_change(sqlite3* db);
- /*sqlite3* db=NULL;*/
-
- int main(int argc, const char *argv[])
- {
- sqlite3* db=NULL;
- if(sqlite3_open("./my.db",&db)){
- printf("%s\n",sqlite3_errmsg(db));
- return -1;
- }
- printf("sqlite3_open success\n");
- char str[128]="create table if not exists stu (id int key,name char,score int)";
- char*errmsg=NULL;
- if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"create failed __%d__\n",__LINE__);
- return -1;
- }
- char c=0;
- while(1){
- system("clear");
- printf("------------------\n");
- printf("------------------\n");
- printf("------1.insert----\n");
- printf("------2.delet-----\n");
- printf("------3.change----\n");
- printf("------4.select----\n");
- printf("------5.quit------\n");
- printf("------------------\n");
- c=getchar();
- while(getchar()!='\n');
- switch (c){
- case'1':
- do_insert(db);
- break;
- case'2':
- do_delet(db);
- break;
- case'3':
- do_change(db);
- break;
- case'4':
- do_select(db);
- break;
- case'5':
- goto END;
- }
- fprintf(stderr,"input space clear>>");
- while(getchar()!='\n');
- }
- END:
- sqlite3_close(db);
- return 0;
- }
- int do_insert(sqlite3* db){
- int id=0;
- char name[20]="";
- int score=0;
- printf("please input id>>");
- scanf("%d",&id);
- while(getchar()!='\n');
-
- printf("please input name>>");
- scanf("%s",name);
- while(getchar()!='\n');
-
- printf("please input score>>");
- scanf("%d",&score);
- while(getchar()!='\n');
-
- char str[128]="";
- sprintf(str,"INSERT INTO stu values(%d, \"%s\",%d);",id,name,score);
-
- char*errmsg=NULL;
- if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"insert failed __%d__\n",__LINE__);
- return -1;
- }
- fprintf(stderr,"insert in to stu success\n");
- printf("id=%d,name=%s,score=%d\n",id,name,score);
- return 0;
- }
- int do_delet(sqlite3* db){
- do_select(db);
- int id=0;
- printf("please input the id who needing be deleted>>");
- scanf("%d",&id);
- while(getchar()!='\n');
-
-
- char str[128]="";
- sprintf(str,"delet from stu where id=%d;",id);
-
- char*errmsg=NULL;
- if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
- return -1;
- }
- fprintf(stderr,"delet id=%d from stu success\n",id);
- return 0;
- }
- int do_change(sqlite3* db){
- again:
- do_select(db);
- int id=0;
- char name[20]="";
- int score=0;
- printf("please input the id who needing be changed>>");
- scanf("%d",&id);
- while(getchar()!='\n');
- printf("please input the member from stu who need change:");
- printf("1.name 2.score\n");
- char ctrl=getchar();
- if('1'==ctrl){
- printf("please input the new name:");
- scanf("%s",name);
- char str[128]="";
- sprintf(str,"update stu set name=%s where id=%d;",name,id);
-
- char*errmsg=NULL;
- if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
- return -1;
- }
- fprintf(stderr,"delet id=%d from stu success\n",id);
- }else if('2'==ctrl){
- printf("please input the new score:");
- scanf("%d",&score);
- char str[128]="";
- sprintf(str,"update stu set score=%d where id=%d;",score,id);
-
- char*errmsg=NULL;
- if(sqlite3_exec(db,str,NULL,NULL,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"deletion be failed __%d__\n",__LINE__);
- return -1;
- }
- fprintf(stderr,"delet id=%d from stu success\n",id);
-
- }else{
- printf("input error!\n");
- printf("please input again");
- while(getchar()!='\n');
- goto again;
- }
- printf("change success\n");
- return 0;
- }
- int do_select(sqlite3*db){
- char str[128]={0};
- sprintf(str,"select * from stu;");
- char* errmsg=NULL;
- char**retPtr=NULL;
- int row;
- int column;
- if(sqlite3_get_table(db,str,&retPtr,&row,&column,&errmsg)!=SQLITE_OK){
- fprintf(stderr,"get_table failed __%d__,errno=%s\n",__LINE__,errmsg);
- return -1;
- }
- for(int i=0;i<row;i++){
- for(int j=0;j<column;j++){
- fprintf(stderr,"%-8s\t",*(retPtr+(i*column)+j));
- }
- putchar(10);
- }
- printf("select success\n");
- }
(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的指针,访问数组越界的指针,指向非法内容的指针。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。