一、思维导图
二、
//定义删除函数
int do_delete(sqlite3 *ppDb)
{int del_numb=0;printf("请输入要删除的学生的学号:");scanf("%d",&del_numb);getchar();//准备sql语句char sql[128]="select *from Stu";sprintf(sql,"delete from Stu where numb =%d;",del_numb);//定义接收错误信息的变量char *errmsg=NULL;//执行sql语句if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("删除成功\n");return 0;
}
//定义修改函数
int do_update(sqlite3 *ppDb)
{int upd_numb=0;double new_score=0;printf("请输入要修改的学生的学号:");scanf("%d",&upd_numb);printf("请输入学生的新成绩:");scanf("%lf",&new_score);getchar();//准备sql语句char sql[128]="select *from Stu";sprintf(sql,"update Stu set score =%lf where numb =%d;",new_score,upd_numb);//定义接收错误信息的变量char *errmsg=NULL;//执行sql语句if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("修改成功\n");return 0;
}
完整代码
#include<myhead.h>//封装添加学生信息函数
int do_add(sqlite3 *ppDb)
{//准备sql语句int add_numb = 0;char add_name[20] = "";double add_score = 0;//提示并输入数据printf("请输入学号:");scanf("%d", &add_numb);printf("请输入姓名:");scanf("%s", add_name);printf("请输入成绩:");scanf("%lf", &add_score);getchar();char sql[128] = "";sprintf(sql,"insert into Stu values(%d, \"%s\", %lf);", add_numb,add_name,add_score);//printf("sql = %s\n", sql);//定义接收错误信息的变量char *errmsg = NULL;//执行sql语句if(sqlite3_exec(ppDb, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("exec error:%s\n", errmsg);sqlite3_free(errmsg); //释放错误信息的空间return -1;}printf("插入成功\n");return 0;}//定义删除函数
int do_delete(sqlite3 *ppDb)
{int del_numb=0;printf("请输入要删除的学生的学号:");scanf("%d",&del_numb);getchar();//准备sql语句char sql[128]="select *from Stu";sprintf(sql,"delete from Stu where numb =%d;",del_numb);//定义接收错误信息的变量char *errmsg=NULL;//执行sql语句if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("删除成功\n");return 0;
}//定义修改函数
int do_update(sqlite3 *ppDb)
{int upd_numb=0;double new_score=0;printf("请输入要修改的学生的学号:");scanf("%d",&upd_numb);printf("请输入学生的新成绩:");scanf("%lf",&new_score);getchar();//准备sql语句char sql[128]="select *from Stu";sprintf(sql,"update Stu set score =%lf where numb =%d;",new_score,upd_numb);//定义接收错误信息的变量char *errmsg=NULL;//执行sql语句if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("修改成功\n");return 0;
}//定义回调函数处理查询后的结果集
int callback(void *arg,int cols,char **msgText,char **msgHeader)
{//输出所有数据//输出表头if(*(int *)arg == 0){for(int i=0;i<cols;i++){printf("%s\t",*(msgHeader+i));}printf("\n");(*((int *)arg)) ++;}//输出当前记录的内容for(int i=0;i<cols;i++){printf("%s\t",*(msgText+i));}printf("\n");return 0;
}//定义查询函数
int do_search(sqlite3 *ppDb)
{//准备sql语句char sql[128] = "select * from Stu;";//定义错误信息容器char *errmsg = NULL;int flag = 0; //定义标识位//执行sql语句if(sqlite3_exec(ppDb, sql, callback, &flag, &errmsg) != SQLITE_OK){printf("exec error:%s\n", errmsg);sqlite3_free(errmsg); //释放错误信息的空间return -1;}return 0;
}
//定义查询函数1
int do_search_1(sqlite3 *ppDb)
{//准备sql语句char sql[128] = "select *from Stu;";//定义错误信息容器char *errmsg = NULL;//准备接收结果集的相关变量char **retPtr = NULL; //记录结果集的起始地址int rows = 0; //记录结果集的行数int cols = 0; //记录结果集的列数//执行sql语句if(sqlite3_get_table(ppDb,sql,&retPtr,&rows,&cols,&errmsg)!=SQLITE_OK){printf("get table error:%s\n",errmsg);sqlite3_free(errmsg);return -1;}//此时查询的结果集合已经放入了retPtr作为起始地址的二维容器里面for(int i=0;i<rows+1;i++){for(int j=0;j<cols;j++){printf("%s\t",*(retPtr + (i*cols)+j)); //输出任意一个元素}printf("\n");}//释放结果集sqlite3_free_table(retPtr);return 0;
}/***************************主程序***********************************/
int main(int argc, const char *argv[])
{//1、定义数据库句柄指针sqlite3 *ppDb = NULL;//2、打开(创建)数据库if(sqlite3_open("./my.db",&ppDb) != SQLITE_OK){fprintf(stderr,"sqlite_open error:errmsg = %s,errcode = %d\n",sqlite3_errmsg(ppDb),sqlite3_errcode(ppDb));return -1;}printf("sqlite3_open success\n");//3、创建数据库表//3.1、准备sql语句char sql[128] = "create table if not exists Stu(numb int,name char,score double);";char *errmsg =NULL; //用于接收出错时的错误消息//3.2执行sql语句if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg) != SQLITE_OK){printf("exec error:%s\n,errmsg");sqlite3_free(errmsg); //释放错误信息空间return -1;}printf("create table Stu success\n");//4、对数据表进行操作int menu = -1;while(1){system("clear");printf("\t\t============XXX 学生管理系统=============\n");printf("\t\t============1、添加学生信息=============\n");printf("\t\t============2、删除学生信息=============\n");printf("\t\t============3、修改学生信息=============\n");printf("\t\t============4、查找学生信息=============\n");printf("\t\t============0、退出系统=============\n");printf("请输入功能:");scanf("%d",&menu);getchar();//对功能多分支选择switch(menu){case 1:{do_add(ppDb);}break;case 2:{do_delete(ppDb);}break;case 3:{do_update(ppDb);}break;case 4:{do_search_1(ppDb);}break;case 0:goto END;default:printf("您输入的功能有误,请重新输入!\n");}printf("请输入任意键,按回车清屏\n");while(getchar() !='\n');}END://关闭数据库sqlite3_close(ppDb);return 0;
}