首页 > 代码库 > 基于数据库MySQL的简易学生信息管理系统

基于数据库MySQL的简易学生信息管理系统

通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统。

 

一:基本要求

 1.通过已知用户名和密码进行登录;

 2.可以显示菜单;

 3.可以随时插入学生信息;

 4.可以删除学生信息;

 5.可以通过学生姓名或学号显示学生所有信息;

 还可以修改学生信息,添加学生表格属性等等,,,这些实现都基本类似上述的(这些不想写了,最简易的学生信息管理系统);

 

二:步骤

    1.写一个sql脚本,包括创建数据库,使用数据库,创建学生信息表格,插入大部分学生信息。

    student_information.sql

create database school_db character set gbk;use school_db;create table students (        student_id int unsigned not null primary key,        name       char(10) not null,        sex        char(4) not null,        birth      date    not null,        politily   char(20) not null);insert into students values (201080701,"张三","","1994-12-2","自动化1");insert into students values (201080702,"李四","","1989-10-8","自动化2");insert into students values (201080703,"王五","","1994-8-8","自动化2");insert into students values (201080704,"路人1","","1994-7-9","自动化2");insert into students values (201080705,"路人2","","1994-12-8","自动化2");insert into students values (201080706,"路人3","","1994-11-8","自动化2");insert into students values (201080707,"路人4","","1992-1-23","自动化2");

。。。。

  

     2.编写代码

  

技术分享
#include <stdlib.h>#include <stdio.h>#include <mysql.h>#include <ctype.h>#include <curses.h>//#define MAX_PASSWD_LEN 16void printf_mune(void);char *getPasswd(void);int insert_student(void);int delete_student(void);int inquiry_student(void);int main(int argc,char** argv){    char user[20];    char passwd[20];    char c;    int i;    printf("***********************************************************\n");    printf("*      welcome to student information managent system     *\n");    printf("***********************************************************\n");//    printf("             user: ______________\b\b\b\b\b\b\b\b\n");//    printf("            psaawd:______\b\b\b\b\b\b\n");    while(1){                printf("                    user:");        scanf("%s", user);         printf("                   passwd:");             scanf("%s",passwd);             /*  while ((c=getch())!=‘\n‘)        {            if (i<MAX_PASSWD_LEN && isprint(c))   //isprint函数判断c是否是可打印字符            {                passwd[i++] = c;                putchar(‘*‘);            }            else if (i>0 && c==‘\b‘)            {                --i;                putchar(‘\b‘);                putchar(‘ ‘);                putchar(‘\b‘);            }        }        putchar(‘\n‘);        passwd[i] = ‘\0‘; */        if(!strncmp(user, "qigaohua", 8)){            if(!strncmp(passwd, "123456", 6))                break;            else {                printf("passwd error\n");                continue;            }        }else{            printf("user error\n");            continue;        }    }    printf("login sucess\n");    while(1){         printf_mune();         getchar();                   //问题1  必须加         c = fgetc(stdin);         switch(c){            case I:                insert_student();                break;            case D:                delete_student();                break;            case S:                inquiry_student();                break;            case Q:                return 0;                break;            default:                ;             }    }    printf("exit sucess\n");    return 0;    }/*getc()和getchar()函数想必大家都经常用到,但它们都在输入的同时显示输入内容,并由回车终止输入。为了不显示输入内容,我们调用另外一个函数getch(),它包含在头文件中。该函数可以在输入的同时不显示输入内容,并在输入完成后不需回车而自动终止输入。与此同时,该头文件中还包含另外一个函数getche(),它和getch()功能相同,唯一的区别是输入的同时显示输入的内容。本文我们只用到getch();*//*char *getPasswd(){    unsigned char c;    passwd =(char*)malloc(8);    int i = 0;    while ((c=getch())!=‘\r‘)    {        if (i<MAX_PASSWD_LEN && isprint(c))   //isprint函数判断c是否是可打印字符        {            passwd[i++] = c;            putchar(‘*‘);        }        else if (i>0 && c==‘\b‘)        {            --i;            putchar(‘\b‘);            putchar(‘ ‘);            putchar(‘\b‘);        }    }    putchar(‘\n‘);    passwd[i] = ‘\0‘;        return passwd;}*/void printf_mune(void){    printf("The following operations can be performed\n");    printf("1. insert student information             input I\n");    printf("2. delete student information                    input D\n");    printf("3. inquiry student information                   input S\n");    printf("4. exit student information managent system      input Q\n");}int insert_student(void){    MYSQL mysql;    char sql[100];    char k;    char id[10], name[20], sex[5],birth[15],potility[20];    mysql_init(&mysql);    if(!mysql_real_connect(&mysql, "localhost", "root",NULL,"school_db",0,NULL,0)){        printf("ERROR: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));        exit(-1);    }    mysql_set_character_set(&mysql, "utf8");    while(1) {                printf("please input student information\n");        printf("student_id: ");        fflush(stdin);        scanf("%s",id);//        fgets(id, 10, stdin);        printf("\nname: ");        scanf("%s",name);//        fgets(name, 20, stdin);        printf("\nsex: ");        scanf("%s",sex);        printf("\nbirth: ");        scanf("%s",birth);//        fgets(birth, 15, stdin);        printf("\npolitily: ");        scanf("%s",potility);//        fgets(potility, 20, stdin);        printf("\nplease input y or n or q\n");//        read(stdin, k, 1);           getchar();                       //注意要加        k = fgetc(stdin);        if(k == n)            continue;        else if(k == q)            break;        else if(k == y){            sprintf(sql, "insert into students values (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\");", id, name,sex, birth, potility);            printf("\n%s",sql);            if(mysql_query(&mysql, sql)){                printf("\ninsert failed: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));                continue;            }            printf("\ninsert sucess\n");            printf("you want exit? input q\n");            getchar();            k = fgetc(stdin);            if(k == q)                break;        }        }    mysql_close(&mysql);    return 0;}int delete_student(void){    MYSQL mysql;    char sql[100];    char id[10], name[20];    char k;    mysql_init(&mysql);    if(!mysql_real_connect(&mysql, "localhost", "root",NULL,"school_db",0,NULL,0)){        printf("ERROR: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));        exit(-1);    }        mysql_set_character_set(&mysql, "utf8");    while(1){        printf("please input you want delete student id or name, selset i or n or q\n");        getchar();              //注意        k = fgetc(stdin);        if(k == i) {            printf("student id: ");            scanf("%s",id);            //fgets(id, 10, stdin);            sprintf(sql, "delete from students where student_id=\"%s\";", id);        }else if(k == n){            printf("\nstudent name: ");            scanf("%s",name);            //fgets(name, 20, stdin);            sprintf(sql, "delete from students where name=\"%s\";", name);                    }else if(k == q)            break;        printf("\nyou are sure? y or n or q\n");        getchar();        k = fgetc(stdin);        if(k == n)            continue;        else if(k == y){            if(mysql_query(&mysql, sql)){                printf("delete failed: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));                continue;            }            printf("delete sucess\n");            }        else if(k == q)            break;    }    mysql_close(&mysql);    return 0;}int inquiry_student(void){    MYSQL mysql;    MYSQL_RES *res;    MYSQL_ROW  row;    MYSQL_FIELD *filed;    char sql[100];    char id[10], name[20];    char k;        int column,i;    my_ulonglong backrows;    mysql_init(&mysql);    if(!mysql_real_connect(&mysql, "localhost", "root",NULL,"school_db",0,NULL,0)){        printf("ERROR: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));        exit(-1);    }        mysql_set_character_set(&mysql, "utf8");        while(1){        printf("please input you want inquiry student id or name, selset i or n or q\n");        getchar();   //标记        k = fgetc(stdin);        if(k == i) {            printf("student id: ");            scanf("%s", id);            //fgets(id, 10, stdin);            sprintf(sql, "select * from students where student_id=\"%s\";", id);            printf("sql request:%s\n", sql);        }else if(k == n) {            printf("\nstudent name: ");            scanf("%s", name);            //fgets(name, 20, stdin);            sprintf(sql, "select * from students where name=\"%s\";", name);            printf("sql request:%s\n", sql);        }else if(k == q)            break;        printf("\nyou are sure? y or n or q\n");        getchar();        k = fgetc(stdin);        if(k == n)            continue;        else if(k == y){            if(mysql_query(&mysql, sql)){                printf("delete failed: %d--%s\n", mysql_errno(&mysql), mysql_error(&mysql));                continue;            }                        res = mysql_store_result(&mysql);   //首先判断res是否为NULL来判断是否有数据,但是失败了,不知道为什么,最后用了下面函数            backrows = mysql_num_rows(res);//返回上面函数返回结果的行数                        if(backrows != 0){                                column = mysql_num_fields(res);                while((row = mysql_fetch_row(res))){                    i = 0;                    while((filed = mysql_fetch_field(res)) != NULL && (i < column)){                        printf("%s:", filed->name);                        printf("%s", row[i++]);                        printf("\n");                    }                    printf("\n");                }            }else{                printf("no data found\n");                continue;            }                        printf("inquiry sucess\n");            }        else if(k == q)            break;    }    mysql_close(&mysql);    return 0;}
View Code

 

  

   3.编译

   gcc -o students students.c -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient -lcurses

  

三:运行现象

  技术分享

     技术分享

    技术分享

     技术分享

    最后选择Q退出整个系统

 

虽然所要求的功能基本实现,但是比预先心里所想的还有许多差距,看看程序就知道,改了有改,确实没时间了,就这样吧。

 

基于数据库MySQL的简易学生信息管理系统