学生管理系统系统
#include <myhead.h>
int do_add(sqlite3 *ppDb)
{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);char *errmsg =NULL;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)
{char add_name[20]="";printf("请输入想删除的学生的名字:");scanf("%s",add_name);getchar();char sql[128]="";sprintf(sql,"delete from stu where name=\"%s\";",add_name);char *errmsg =NULL;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_update(sqlite3 *ppDb)
{char add_name[20]="";printf("请输入想修改学生的名字:");scanf("%s",add_name);printf("*****选择要修改的信息*****\n");printf("**********1.numb**********\n");printf("**********2.score*********\n");printf("**********3.name**********\n");int inf;printf("请选择要修改的信息:");scanf("%d",&inf);char num[20]="";printf("请输入想修改的数值:");scanf("%s",num);getchar();char sql[128]="";switch(inf){case 1:{sprintf(sql,"update stu set numb=\"%s\" where name=\"%s\";",num,add_name);char *errmsg =NULL;if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec error:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("修改成功\n");}break;case 2:{sprintf(sql,"update stu set score=\"%s\" where name=\"%s\";",num,add_name);char *errmsg =NULL;if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec error:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("修改成功\n");}break;case 3:{sprintf(sql,"update stu set name=\"%s\" where name=\"%s\";",num,add_name);char *errmsg =NULL;if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK){printf("exec error:%s\n",errmsg);sqlite3_free(errmsg);return -1;}printf("修改成功\n");}break;default:{printf("输入错误,请重试\n");}break;}return 0;
}
int do_search(sqlite3 *ppDb)
{char sql[128] = "select * from stu;";char *errmsg = NULL;char **retPtr = NULL; int rows = 0; int cols = 0; if(sqlite3_get_table(ppDb, sql, &retPtr, &rows, &cols, &errmsg) != SQLITE_OK){printf("get table error:%s\n", errmsg);sqlite3_free(errmsg);return -1;}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[])
{sqlite3 *ppDb=NULL;if(sqlite3_open("./my.db",&ppDb)!=SQLITE_OK){fprintf(stderr,"sqlite3_open error:errmsg=%s,errcode=%d\n",sqlite3_errmsg(ppDb),sqlite3_errcode(ppDb));return -1;}printf("sqlite3_open success\n");char sql[128]="create table if not exists stu(numb int,name char,score double);";char *errmsg =NULL;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");
int menu = -1;while(1){system("clear");printf("\t\t===============学生管理系统=============\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(ppDb);}break;case 0:goto END;default:printf("您输入的功能有误,请重新输入!!!!\n");}printf("请输入任意键,按回车清屏\n");while(getchar() != '\n');}
END:sqlite3_close(ppDb); return 0;
}