#include<myhead.h>
int do_add(sqlite3 *ppDb)
{
int add_numb = 0;
char add_name[20] = "";
int add_score = 0;
printf("请输入学号:");
scanf("%d",&add_numb);
//getchar();
printf("请输入姓名:");
scanf("%s",add_name);
printf("请输入成绩:");
//getchar();
scanf("%d",&add_score);
getchar();
char sql[128] = "";
sprintf(sql,"insert into Stu values(%d,\"%s\",%d);",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)
{
int del_numb = 0;
printf("请输入学号:");
scanf("%d",&del_numb);
getchar();
char sql[128] = "";
sprintf(sql,"delete from Stu where numb==%d;",del_numb);
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)
{
int updata_numb = 0;
char updata_name[20] = "";
int updata_score = 0;
printf("请输入要修改的numb:");
scanf("%d",&updata_numb);
getchar();
//printf("请输入要修改的name:");
//scanf("%s",&updata_name);
//getchar();
printf("请输入要修改的score:");
scanf("%d",&updata_score);
getchar();
char sql[128] = "";
sprintf(sql,"update Stu set score=%d where numb=%d;",updata_score,updata_numb);
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 callback(void *arg,int cols,char **msgTxet,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",*(msgTxet+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_search_1(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[])
{
//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;
}