思维导图
数据库编程实现学生管理系统
#include <header.h>
#define ID 1
#define NAME 2
#define AGE 3
#define SCORE 4
int do_add(sqlite3 *ppdb)
{
int add_numb;
char add_name[20];
int add_age;
double add_score;
printf("enter student id:");
scanf("%d",&add_numb);
printf("enter student name:");
scanf("%s",add_name);
printf("enter student age:");
scanf("%d",&add_age);
printf("enter student score:");
scanf("%lf",&add_score);
getchar();
char sql[128]="";
snprintf(sql,sizeof(sql),"insert into stu values(%d,\"%s\",%d,%.2lf);",add_numb,add_name,add_age,add_score);
char *errmsg=NULL;
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu create fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("table stu create success\n");
return 0;
}
int callback(void* arg,int cols,char** col_text,char** col_name)
{
if(*(int *)arg==0)
{
for(int i=0;i<cols;i++)
{
printf("%s\t",col_name[i]);
}
printf("\n");
*(int *)arg=1;
}
for(int i=0;i<cols;i++)
{
printf("%s\t",col_text[i]);
}
printf("\n");
return 0;
}
int do_select(sqlite3 *ppdb)
{
int flag=0;
char sql[128]="select * from stu;";
char *errmsg=NULL;
if(sqlite3_exec(ppdb,sql,callback,&flag,&errmsg)!=SQLITE_OK)
{
printf("stu select fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("select have done\n");
return 0;
}
int do_select_2(sqlite3 *ppdb)
{
char sql[128]="select * from stu;";
char **pazresult=NULL;
int cols=0;
int rows=0;
char *errmsg=NULL;
if(sqlite3_get_table(ppdb,sql,&pazresult,&rows,&cols,&errmsg)!=SQLITE_OK)
{
printf("stu select fail:%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",pazresult[i*cols+j]);
}
printf("\n");
}
sqlite3_free_table(pazresult);
return 0;
}
int do_delete(sqlite3 *ppdb)
{
int id;
printf("enter the student id u want to delete:");
scanf("%d",&id);
getchar();
char sql[128]="";
char *errmsg=NULL;
snprintf(sql,sizeof(sql),"delete from stu where id=%d;",id);
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu delete fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("delete have done\n");
return 0;
}
int do_update(sqlite3 *ppdb)
{
int id;
int newid;
char name[20];
int age;
double score;
int column;
printf("enter the id u want to update:");
scanf("%d",&id);
getchar();
char sql[128]="";
char *errmsg=NULL;
printf("enter the column u want to update:");
scanf("%d",&column);
getchar();
switch(column)
{
case 1:
{
printf("enter the new id:");
scanf("%d",&newid);
getchar();
snprintf(sql,sizeof(sql),"update stu set id = %d where id = %d;",newid,id);
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu update fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("update have done\n");
}
break;
case 2:
{
printf("enter the new name:");
scanf("%s",name);
getchar();
snprintf(sql,sizeof(sql),"update stu set name = \"%s\" where id = %d ;",name,id);
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu update fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("update have done\n");
}
break;
case 3:
{
printf("enter the new age:");
scanf("%d",&age);
getchar();
snprintf(sql,sizeof(sql),"update stu set age = %d where id = %d;",age,id);
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu update fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("update have done\n");
}
break;
case 4:
{
printf("enter the new score:");
scanf("%lf",&score);
getchar();
snprintf(sql,sizeof(sql),"update stu set score = %.2lf where id = %d;",score,id);
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu update fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("update have done\n");
}
break;
default:printf("argument error\n");
break;
}
return 0;
}
int main(int argc, const char *argv[])
{
sqlite3 *ppdb=NULL;
if(sqlite3_open("02.db",&ppdb)!=SQLITE_OK)
{
printf("open error:%d,%s\n",sqlite3_errcode(ppdb),sqlite3_errmsg(ppdb));
return 1;
}
printf("the sqlite has been opened\n");
char sql[128]="create table if not exists stu(id int,name text,age int,score real);";
char *errmsg=NULL;
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("stu create fail:%s\n",errmsg);
sqlite3_free(errmsg);
return 1;
}
printf("table stu create success\n");
int menu=0;
while(1)
{
system("clear");
printf("\t\t========student information mange system===========\n");
printf("\t\t============= 1.add ================\n");
printf("\t\t============= 2.delete ================\n");
printf("\t\t============= 3.update ================\n");
printf("\t\t============= 4.select ================\n");
printf("\t\t============= 5.exit ================\n");
printf("enter ur choice:");
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_select(ppdb);
do_select_2(ppdb);
}
break;
case 5:goto END;
default:printf("error argument\n");
}
printf("enter \"enter\" to clear the scream\n");
while(getchar()!='\n');
}
END:
sqlite3_close(ppdb);
return 0;
}