#include<myhead.h>
int do_add(sqlite3 *ppDb)
{
int xue=0;
char name[20]="";
double coure=0;
char sql[128]="";
printf("请输入学号\n");
scanf("%d",&xue);
printf("请输入姓名\n");
scanf("%s",name);
printf("请输入成绩");
scanf("%lf",&coure);
getchar();
sprintf(sql,"insert into stu values(%d, \"%s\",%lf);",xue,name,coure);
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("插入成功");
return 0;
}
int callback(void *arg,int cols,char **masText,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",*(masText+i));
}
printf("\n");
return 0;
}
}
int do_search(sqlite3 *ppDb)
{
char sql[128]="select *from stu;";
char *errmsg=NULL;
int flag=0;
if(sqlite3_exec(ppDb,sql,callback,&flag,&errmsg)!=SQLITE_OK)
{
printf("exec error:%s\n",errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
int do_delete(ppDb)
{
char name[20]="";
char sql[128]="";
printf("请输入要删除人");
scanf("%s",name);
sprintf(ppDb,"delete stu set name==%s;",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;
}
return 0;
}
int do_update(ppDb)
{
char name[20]="";
int xue=0;
double coure=0;
char sql[128]="";
printf("输入名字\n");
scanf("%s",name);
printf("需要更改的学号\n");
scanf("%d",&xue);
printf("输入要修改的分\n数");
scanf("%lf",&coure);
sprintf(sql,"update stu set name=="%s" where xue==%d AND coure==%d;",name,xue,coure);
char *errmsg=NULL;
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("exec error:%s\n",errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
int main(int argc, const char *argv[])
{
//1、定义数据库句柄指针
sqlite3 *ppDb = NULL;
//2、打开(创建)数据库
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");
//3、创建数据库表
//3.1 准备sql语句
char sql[128] = "create table if not exists Stu(numb int, name char, score double);";
//char *sql = "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);
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(ppDb);
}
break;
case 0:goto END;
default:printf("您输入的功能有误,请重新输入!!!!\n");
}
printf("请输入任意键,按回车清屏\n");
while(getchar() != '\n');
}
END:
//关闭数据库
sqlite3_close(ppDb);
return 0;
}