做一个简单数据库终端操作系统
#include <myhead.h>
int main(int argc, const char *argv[])
{int id;char name[16];float score;sqlite3 *p=NULL;if(sqlite3_open("./my.db",&p)!=SQLITE_OK){printf("sqlite3_open error\n");return -1;} int flag;char cmd[256]={0};char *errmsg=NULL;//创建表strcpy(cmd,"create table if not exists stu(id int primary key,name char,score float);");if(sqlite3_exec(p,cmd,NULL,NULL,&errmsg)!=SQLITE_OK){printf("创建表失败\n");return -1;}while(1){system("clear");printf("=============XXX 学生管理系统================\n");printf("=============1. 添加学生信息=================\n");printf("=============2. 删除学生信息=================\n");printf("=============3. 修改学生信息=================\n");printf("=============0. 退出系统=====================\n");printf("请输入操作:");scanf("%d",&flag);while(getchar()!=10);memset(cmd,0,sizeof(cmd));switch(flag){case 1:printf("请输入学号:");scanf("%d",&id);while(getchar()!=10);printf("请输入姓名:");scanf("%s",name);while(getchar()!=10);printf("请输入成绩:");scanf("%f",&score);while(getchar()!=10);sprintf(cmd,"insert into stu values(%d,\"%s\",%f);",id,name,score);if(sqlite3_exec(p,cmd,NULL,NULL,&errmsg)!=SQLITE_OK){printf("%s\n",errmsg);}break;case 2:printf("请输入要删除的学生学号:");scanf("%d",&id);sprintf(cmd,"delete from stu where id=%d;",id);if(sqlite3_exec(p,cmd,NULL,NULL,&errmsg)!=SQLITE_OK){printf("%s\n",errmsg);}break;case 3:printf("请输入学号:");scanf("%d",&id);while(getchar()!=10);printf("请输入姓名:");fgets(name,sizeof(name),stdin);name[strlen(name)-1]='\0';printf("请输入成绩:");scanf("%f",&score);while(getchar()!=10);sprintf(cmd,"update stu set name=\"%s\",score=%f where id=%d;",name,score,id);if(sqlite3_exec(p,cmd,NULL,NULL,&errmsg)!=SQLITE_OK){printf("%s\n",errmsg);}break;case 0:goto end;default:printf("输入错误请重新输入\n");break;}printf("输入回车清屏重新操作\n");while(getchar()!=10);}
end:sqlite3_close(p);return 0;
}