概述 平台:linux 开发语言:c 需要 sqlite3 软件环境 在程序目录先建立 数据文件: 在终端输入命令:sqlite3 student.db --》打开sqlite3 并建立数据文件student.db 在sqlite3 中创建数据表:create table student(id integer primary key, name text, sex text,
平台:linux
开发语言:c
需要 sqlite3 软件环境
在程序目录先建立 数据文件:
在终端输入命令:sqlite3 student.db--》打开sqlite3 并建立数据文件student.db
在sqlite3 中创建数据表:create tablestudent(ID integer primary key,name text,sex text,age text,Chinese integer,English integer,Math integer,avergae integer);
表中一共8个字段:ID, name ,sex,age,语文成绩,英语成绩,数学成绩,平均成绩
#include
#include
#include
#include
#include
#include
#define filename "student.db"
voID menu_print(voID)
{
printf("==================================\n");
printf("1.insert one student's info.\n");
printf("2.delete one student's info.\n");
printf("3.display all student's info.\n");
printf("4.sort student's info.\n");
printf("5.quit.\n");
printf("------------Please input your choice [1-5] :");
}
voID delete_menu(voID)
{
printf("\n");
printf("\t\t1.delete by name.\n");
printf("\t\t2.delete by ID.\n");
printf("\n");
}
int delete_back(voID *p,int argc,char *argv[],char *argvv[])
{
*(int *)p = 0;
return 0;
}
int display_back(voID *p,char *argvv[])
{
int i;
*(int *)p = 0;
printf("%-3s",argv[0]?argv[0]:"null");
printf(" %-16s",argv[1]?argv[1]:"null");
printf(" %-7s",argv[2]?argv[2]:"null");
printf(" %-4s",argv[3]?argv[3]:"null");
printf(" %-5s",argv[4]?argv[4]:"null");
printf(" %-5s",argv[5]?argv[5]:"null");
printf(" %-5s",argv[6]?argv[6]:"null");
printf(" %-5s",argv[7]?argv[7]:"null");
printf("\n");
return 0;
}
voID insert_info(voID)
{
int ret;
int ID;
int age;
int score[4];
char name[64];
char sex[16];
char *err;
char *sql;
sqlite3 *db;
printf("input your infomation.\n");
printf("name : ");
scanf("%s",name);
printf("sex : ");
scanf("%s",sex);
printf("age : ");
scanf("%d",&age);
printf("Chinese sorce : ");
scanf("%d",&score[0]);
printf("English sorce : ");
scanf("%d",&score[1]);
printf("Math sorce : ");
scanf("%d",&score[2]);
score[3] = (score[0] + score[1] + score[2])/3;
ret = sqlite3_open_v2(filename,&db,sqlITE_OPEN_READWRITE,NulL);
if (ret != sqlITE_OK) {
fputs(sqlite3_errmsg(db),stderr);
putchar('\n');
exit(1);
}
sql = sqlite3_mprintf("insert into student(name,sex,age,Chinese,English,Math,"\
"avergae) values(%Q,%Q,%d,%d);",
name,score[0],score[1],score[2],score[3]);
printf("%s\n",sql);
ret = sqlite3_exec(db,sql,NulL,&err);
if (ret != sqlITE_OK) {
fputs(err,stderr);
putchar('\n');
exit(1);
}
sqlite3_free(sql);
sqlite3_close(db);
}
voID delete_by_name(voID)
{
int ret;
int p = 1;
char name[64];
char *err = NulL;
char *sql = NulL;
sqlite3 *db;
ret = sqlite3_open_v2(filename,stderr);
printf("\n");
exit(1);
}
printf("Entry the name you want to delete : ");
scanf("%s",name);
sql = sqlite3_mprintf("select * from student where name=%Q;",name);
ret = sqlite3_exec(db,delete_back,&p,stderr);
putchar('\n');
exit(1);
} else if (p == 1) {
printf("No such name!\n");
sqlite3_free(sql);
sleep(1);
return;
}
sqlite3_free(sql);
sql = sqlite3_mprintf("delete from student where name=%Q;",name);
printf("%s\n",sql);
ret = sqlite3_exec(db,stderr);
putchar('\n');
exit(1);
}
sqlite3_free(sql);
sqlite3_close(db);
}
voID delete_by_ID(voID)
{
int ret,ID;
int p = 1;
char *err = NulL;
char *sql = NulL;
sqlite3 *db;
ret = sqlite3_open_v2(filename,stderr);
printf("\n");
exit(1);
}
printf("Entry the name you want to delete : ");
scanf("%d",&ID);
sql = sqlite3_mprintf("select * from student where name=%d;",ID);
ret = sqlite3_exec(db,stderr);
putchar('\n');
exit(1);
} else if (p == 1) {
printf("No such ID!\n");
sqlite3_free(sql);
sleep(1);
return;
}
sqlite3_free(sql);
sql = sqlite3_mprintf("delete from student where name=%Q;",ID);
printf("%s\n",stderr);
putchar('\n');
exit(1);
}
sqlite3_free(sql);
sqlite3_close(db);
}
int is_read(voID)
{
int i;
ioctl(0,FIONREAD,&i);
return i;
}
voID delete_choice(voID)
{
int delet,i;
char tmp = 'n';
char buf[1024];
while (1) {
delete_menu();
gets(buf);
if (strlen(buf) > 1 || buf[0] < '0' || buf[0] > '3') {
printf("Can't recognition your input message,input again!\n");
printf("\t\tpush Y(continue)\n\t\tpush N(cancle).\n");
system("stty raw");
while (1) {
char ch;
while (is_read() == 0) {
}
ch = getchar();
if ((ch == 'Y' || ch == 'y') && i == 1) {
break;
} else if ((ch == 'n'|| ch == 'N') && i == 1) {
system("stty cooked");
return;
} else if (ch == 13){
printf("\r "
" \r");
fflush(stdout);
i = 0;
}
i++;
}
system("stty cooked");
} else if (buf[0] > '0' && buf[0] < '3') {
delet = buf[0] - '0';
break;
}
}
if (delet == 1) {
delete_by_name();
} else if (delet == 2){
delete_by_ID();
}
}
voID display_info(voID)
{
int ret;
int p = 1;
char *sql;
char *err;
sqlite3 *db;
ret = sqlite3_open_v2(filename,stderr);
printf("\n");
exit(1);
}
printf("---------------------------------------------------------------------\n");
printf("%-3s %-16s %-7s %-4s %-8s %-8s %-8s %-8s\n",
"ID","name","sex","age","Chinese","English","Math","average");
ret = sqlite3_exec(db,"select * from student;",display_back,stderr);
printf("\n");
exit(1);
}
printf("---------------------------------------------------------------------\n");
sqlite3_close(db);
}
voID sort_info(voID)
{
int ret;
int p = 1;
char *err;
sqlite3 *db;
ret = sqlite3_open_v2(filename,stderr);
exit(1);
}
printf("---------------------------------------------------------------------\n");
printf("%-3s %-16s %-7s %-4s %-8s %-8s %-8s %-8s\n","avergae");
ret = sqlite3_exec(db,"select * from student order by avergae desc;",
display_back,stderr);
printf("\n");
exit(1);
} else if (p == 1) {
printf("Datebase is empty!\n");
return;
}
printf("---------------------------------------------------------------------\n");
sqlite3_close(db);
}
int main(int argc,const char *argv[])
{
int choice = 0;
char buf[1024];
while (choice != 5) {
menu_print();
while (1) {
gets(buf);
if (strlen(buf) > 1) {
printf("Can't recognition your input message,input again!\n\n");
menu_print();
continue;
} else {
if (buf[0] > '0' && buf[0] < '6') {
choice = buf[0]- '0';
break;
} else {
printf("Can't recognition your input message,input again!\n\n");
menu_print();
}
}
}
switch(choice) {
case 1:
insert_info();
gets(buf);
break;
case 2:
delete_choice();
break;
case 3:
display_info();
break;
case 4:
sort_info();
break;
case 5:
printf("\t\t\t\t\tBye...\n");
break;
default:
break;
}
}
return 0; }
总结
以上是内存溢出为你收集整理的学生信息管理系统(sqlite3)全部内容,希望文章能够帮你解决学生信息管理系统(sqlite3)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)