#include <myhead.h>
int main(int argc, char const *argv[])
{
typedef struct {
int num;
char name[32];
double socre;
}data;
sqlite3 *ppDb=NULL;
if(sqlite3_open("./mydb.db",&ppDb)!=SQLITE_OK)
{
printf("sqlite3_open error:%s,errcode=%d\n",sqlite3_errmsg(ppDb),sqlite3_errcode(ppDb));
return -1;
}
printf("数据库打开成功\n");
//创建数据库
// char sql[128]="create table if not exists stu(num int,name char,socre double)";
//创建数据库
char *errmsg=NULL;
char sql[256]="";
int flag;
strcpy(sql,"create table if not exists stu(num int primary key,name char,score double);");
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("数据表创建成功\n");
data buf;
while(1)
{
printf("----学生管理系统----\n");
printf("---1、添加学生信息---\n");
printf("---2、删除学生信息---\n");
printf("---3、修改学生信息---\n");
printf("---4、退出---\n");
printf("选择功能:");
scanf("%d",&flag);
while(getchar()!=10);
memset(sql,0,sizeof(sql));
memset(&buf,0,sizeof(buf));
switch (flag)
{
case 1:
printf("请输入学生信息>>>>");
scanf("%d %s %lf",&buf.num,buf.name,&buf.socre);
while(getchar()!=10);
sprintf(sql,"insert into stuvalues(%d,'%s',%lf);",buf.num,buf.name,buf.socre);
sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
break;
case 2:
printf("请输入要删除学生学号>>>");
scanf("%d",&buf.num);
while(getchar()!=10);
sprintf(sql,"delete from stu where num=%d;",buf.num);
break;
case 3:
printf("请输入要修改学生学号>>>");
scanf("%d",&buf.num);
printf("请输入要修信息>>>");
scanf("%s %lf",buf.name,&buf.socre);
sprintf(sql,"update stu set name=\"%s\",score=%lf where num=%d;",buf.name,buf.socre,buf.num);
break;
default:
break;
}
}
sqlite3_close(ppDb);
return 0;
}
04.18作业
最新推荐文章于 2024-09-02 21:17:55 发布