基于Linux系统sqlite3数据库的学生信息管理系统

学生信息管理系统

表:
	学生信息表   入学的时候
		--学号[主关键字]  姓名  性别  班级  籍贯
	考试成绩表	  考试的时候生产的
		--学号[]	姓名  成绩   班级
	教师表		 老师
		--名字	工号  教龄	班级

功能:
	学生信息添加  删除 根据姓名查找
	教师信息的 添加 删除 查找
	成绩信息的 添加 删除 查找
	联合查询:
		找出成绩不合格学生的老师名字 班级
		查询成绩不合格学生籍贯
		统计所有成绩不合格学生的个数
		计算某个老师班级的平均成绩 [注释1]

注意: 字符串和char 在snprintf中需要加\'xx\';

注释1:

"select score.name,score.chengji,teacher.name,teacher.class from student,teacher  where score.class=teacher.class";//给每个学生找老师
"select score.name,score.chengji,teacher.name,teacher.class from student,teacher where score.class=teacher.class and teacher.name=\'%s\'"; 具体某个老师的所有学生

代码如下:

#include 
#include 
#include 
#include 

static int cnt=1;
static int num=0;
static int s=0;
static int n=0;
static int ave;

//打印学生管理系统主界面
int print(void *para,int f_num,char **f_value,char **f_name)
{
	int i;
	if(cnt)
	{
		printf("--------------------------------------------------\n");
		cnt--;
		for(i=0;i<f_num;i++)
		{
			printf("%-12s",f_name[i]);
		}
		printf("\n");
		printf("--------------------------------------------------\n");
	}
	//打印学生信息
	for(i=0;i<f_num;i++)
	{
	printf("%-11s",f_value[i]);
	}
	printf("\n");
	printf("--------------------------------------------------\n");

	return 0;
}

int fail_num(void *para,int f_num,char **f_value,char **f_name)
{
	num++;
	return 0;
}

int average(void *para,int f_num,char **f_value,char **f_name)
{
	n++;
	s=s+atoi(f_value[0]);
	ave = s/n;
	return 0;
}

//查找所有学生信息
int findall(sqlite3 *db,char *comd)
{
	int res;
	char cmd[128];
	memset(cmd,0,sizeof(*cmd));
	snprintf(cmd,128,"select * from %s;",comd);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	return 0;
}

//在数据库中按姓名查找学生信息
int find(sqlite3 *db,char *comd)
{
	int res;
	char name[16];
	char cmd[128];
	printf("请输入姓名:");
	scanf("%s",name);
	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	return 0;

}

//添加学生信息到数据库的学生信息表中
int add1(sqlite3 *db,char *comd)
{
	int res;
	char name[16];
	int id;
	char sex[4];
	char class[6];
	char home[16];
	char cmd[128];
	printf("请输入姓名:");
	scanf("%s",name);
	printf("请输入学号:");
	scanf("%d",&id);
	printf("请输入性别:");
	scanf("%s",sex);
	printf("请输入班级:");
	scanf("%s",class);
	printf("请输入籍贯:");
	scanf("%s",home);
	snprintf(cmd,128,"insert into %s (姓名,学号,性别,班级,籍贯) values (\"%s\",%d,\"%s\",\"%s\",\"%s\");",comd,name,id,sex,class,home);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	printf("添加成功!\n");

	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	return 0;

}

//添加教师信息到数据库的教师信息表中
int add2(sqlite3 *db,char *comd)
{
	int res;
	char name[16];
	int id;
	char class[6];
	int age;
	char cmd[128];
	printf("请输入姓名:");
	scanf("%s",name);
	printf("请输入工号:");
	scanf("%d",&id);
	printf("请输入班级:");
	scanf("%s",class);
	printf("请输入教龄:");
	scanf("%d",&age);
	snprintf(cmd,128,"insert into %s (姓名,工号,教龄,班级) values (\"%s\",%d,%d,\"%s\");",comd,name,id,age,class);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	printf("添加成功!\n");

	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	return 0;
}

//添加学生考试成绩信息到数据库的考试成绩表
int add3(sqlite3 *db,char *comd)
{
	int res;
	char name[16];
	int id;
	char class[6];
	float score;
	char cmd[128];
	printf("请输入姓名:");
	scanf("%s",name);
	printf("请输入学号:");
	scanf("%d",&id);
	printf("请输入班级:");
	scanf("%s",class);
	printf("请输入成绩:");
	scanf("%f",&score);
	snprintf(cmd,128,"insert into %s (姓名,学号,班级,成绩) values (\"%s\",%d,\"%s\",%2f);",comd,name,id,class,score);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	printf("添加成功!\n");

	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	return 0;
}

//删除学生信息
int delete(sqlite3 *db,char *comd)
{
	int res;
	char name[16];
	char cmd[128];
	printf("请输入姓名:");
	scanf("%s",name);
	snprintf(cmd,128,"delete from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	return 0;

}

/* 	函数名:update
	作用:更改数据库信息
	参数:
		db:需要操作的数据库
		comd:需要操作的数据表(学生信息表、教师信息表、学生成绩表)对应的命令
		table:操作的数据表,分别对应1,2,3
*/
int update(sqlite3 *db,char *comd,int table)
{
	int res;
	char name[16];
	char cmd[128];
	int chose;
	char type[6];
	char data[16];
	printf("请输入姓名:");
	scanf("%s",name);

	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	if(table == 1)
	{
		printf(" ________________________________________\n");
		printf("| 1.姓名  2.学号  3.性别  4.班级  5.籍贯 |\n");
		printf("|________________________________________|\n");
		printf("请选择更改项:");
		scanf("%d",&chose);
		switch(chose)
		{
		case 1:strcpy(type,"姓名");break;
		case 2:strcpy(type,"学号");break;
		case 3:strcpy(type,"性别");break;
		case 4:strcpy(type,"班级");break;
		case 5:strcpy(type,"籍贯");break;
		}
	}

	if(table == 2)
	{
		printf(" ________________________________________\n");
		printf("|     1.姓名  2.工号  3.教龄  4.班级     |\n");
		printf("|________________________________________|\n");
		printf("请选择更改项:");
		scanf("%d",&chose);
		switch(chose)
		{
		case 1:strcpy(type,"姓名");break;
		case 2:strcpy(type,"工号");break;
		case 4:strcpy(type,"班级");break;
		case 3:strcpy(type,"教龄");break;
		}

	}

	if(table == 3)
	{
		printf(" ________________________________________\n");
		printf("|     1.姓名  2.学号  3.班级  4.成绩     |\n");
		printf("|________________________________________|\n");
		printf("请选择更改项:");
		scanf("%d",&chose);
		switch(chose)
		{
		case 1:strcpy(type,"姓名");break;
		case 2:strcpy(type,"学号");break;
		case 3:strcpy(type,"班级");break;
		case 4:strcpy(type,"成绩");break;
		}

	}

	printf("请输入要更改的内容:");
	scanf("%s",data);

	if(strcmp(type,"姓名") == 0)
		strcpy(name,data);

	snprintf(cmd,128,"update %s set %s=\"%s\" where 姓名=\"%s\";",comd,type,data,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	snprintf(cmd,128,"select * from %s where 姓名=\"%s\";",comd,name);

	cnt = 1;
	res = sqlite3_exec(db,cmd,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	return 0;

}

int seven(sqlite3 *db)
{
	int res;

	cnt = 1;
	res = sqlite3_exec(db\
			,"select techer.姓名,techer.班级 from techer,student,sore where techer.班级=student.班级 and sore.成绩<60 and sore.姓名=student.姓名;"\
			,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	return 0;
}

int eight(sqlite3 *db)
{
	int res;

	cnt = 1;
	res = sqlite3_exec(db\
			,"select student.姓名,student.籍贯 from student,sore where sore.成绩<60 and sore.姓名=student.姓名;"\
			,print,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	return 0;
}

int nine(sqlite3 *db)
{
	int res;

	cnt = 1;
	res = sqlite3_exec(db\
			,"select student.姓名 from student,sore where sore.成绩<60 and sore.姓名=student.姓名;"\
			,fail_num,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}
	printf("成绩不合格学生的人数为:%d\n",num);
	num = 0;
	return 0;
}

int ten(sqlite3 *db)
{
	int res;
	char teacher[16];
	char cmd[128];

	printf("请输入老师的姓名:");
	scanf("%s",teacher);
	snprintf(cmd,128,"select sore.成绩 from sore,techer where sore.班级=techer.班级 and techer.姓名=\"%s\";",teacher);

	cnt = 1;
	res = sqlite3_exec(db,cmd,average,NULL,NULL);
	if(res)
	{
		printf("sqlite3_exec():%s\n",sqlite3_errmsg(db));
		exit(-3);
	}

	printf("%s老师班级的平均成绩为:%d\n",teacher,ave);
	s=0;n=0;
	
	return 0;
}


int main(void)
{
	int res;
	sqlite3 *db;
	int chose;
	int table;
	char comd[128];
	res = sqlite3_open("/home/linux/180702/180817数据库/studentinformatin/student.db"\
			,&db);
	if(res)
	{
		printf("sqlite3_open():%s\n",sqlite3_errmsg(db));
		exit(-1);
	}
loop1:
	printf(" _______________________\n");
	printf("|      1.学生信息表     |\n");
	printf("|      2.教师信息表     |\n");
	printf("|      3.学生成绩表     |\n");
	printf("|_______________________|\n");
	printf("请选择表格:");
	scanf("%d",&table);
	switch(table)
	{
	case 1:strcpy(comd,"student");
		   break;
	case 2:strcpy(comd,"techer");
		   break;
	case 3:strcpy(comd,"sore");
		   break;
	}

	while(1)
	{
		printf(" _________________________________________\n");
		printf("|               信息管理系统              |\n");
		printf("|              1.查看所有信息             |\n");
		printf("|              2.查找个人信息             |\n");
		printf("|              3.增加个人信息             |\n");
		printf("|              4.更改个人信息             |\n");
		printf("|              5.删除个人信息             |\n");
		printf("|              6.选择其他表格             |\n");
		printf("|       7.统计不合格学生的老师和班级      |\n");
		printf("|       8.统计不合格学生的籍贯            |\n");
		printf("|       9.统计不合格学生的人数            |\n");
		printf("|       10.统计某个老师班级的平均成绩     |\n");
		printf("|_________________________________________|\n");
		printf("请选择:");
		scanf("%d",&chose);

		switch(chose)
		{
		case 1:findall(db,comd);break;
		case 2:find(db,comd);break;
		case 3:
			   if(table == 1)
				   add1(db,comd);
			   if(table == 2)
				   add2(db,comd);
			   if(table == 3)
				   add3(db,comd);
			   break;
		case 4:update(db,comd,table);break;
		case 5:delete(db,comd);break;
		case 6:goto loop1;break;
		case 7:seven(db);break;
		case 8:eight(db);break;
		case 9:nine(db);break;
		case 10:ten(db);break;
		}
	}

	res = sqlite3_close(db);
	if(res)
	{
		printf("sqlite3_close():%s\n",sqlite3_errmsg(db));
		exit(-2);
	}

	exit(0);
}

你可能感兴趣的:(嵌入式编程)