zoukankan      html  css  js  c++  java
  • 基于数据库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");

    。。。。

      执行脚本:

        第一种方法: mysql -h localhost -u root -p 密码  <  student_information.sql;

        第二种方法:已经登录了mysql,  source  student_information.sql;

         2.编写代码

      

    #include <stdlib.h>
    #include <stdio.h>
    #include <mysql.h>
    #include <ctype.h>
    #include <curses.h>
    
    
    //#define MAX_PASSWD_LEN 16
    
    
    void 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("***********************************************************
    ");
        printf("*      welcome to student information managent system     *
    ");
        printf("***********************************************************
    ");
    //    printf("             user: ______________
    ");
    //    printf("            psaawd:______
    ");
    
        while(1){
            
            printf("                    user:");
            scanf("%s", user);
    
             printf("                   passwd:");
        
             scanf("%s",passwd);
           
          /*  while ((c=getch())!='
    ')
            {
                if (i<MAX_PASSWD_LEN && isprint(c))   //isprint函数判断c是否是可打印字符
                {
                    passwd[i++] = c;
                    putchar('*');
                }
                else if (i>0 && c=='')
                {
                    --i;
                    putchar('');
                    putchar(' ');
                    putchar('');
                }
            }
            putchar('
    ');
            passwd[i] = ''; */
    
            if(!strncmp(user, "qigaohua", 8)){
                if(!strncmp(passwd, "123456", 6))
                    break;
                else {
                    printf("passwd error
    ");
                    continue;
                }
            }else{
                printf("user error
    ");
                continue;
            }
        }
    
        printf("login sucess
    ");
        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
    ");
        return 0;
        
    }
    
    /*getc()和getchar()函数想必大家都经常用到,但它们都在输入的同时显示输入内容,并由回车终止输入。
    为了不显示输入内容,我们调用另外一个函数getch(),它包含在头文件中。该函数可以在输入的同时不显示
    输入内容,并在输入完成后不需回车而自动终止输入。与此同时,该头文件中还包含另外一个函数getche(),
    它和getch()功能相同,唯一的区别是输入的同时显示输入的内容。本文我们只用到getch();*/
    /*char *getPasswd()
    {
        unsigned char c;
        passwd =(char*)malloc(8);
        int i = 0;
    
        while ((c=getch())!='
    ')
        {
            if (i<MAX_PASSWD_LEN && isprint(c))   //isprint函数判断c是否是可打印字符
            {
                passwd[i++] = c;
                putchar('*');
            }
            else if (i>0 && c=='')
            {
                --i;
                putchar('');
                putchar(' ');
                putchar('');
            }
        }
        putchar('
    ');
        passwd[i] = '';
        
        return passwd;
    }*/
    
    
    void printf_mune(void)
    {
        printf("The following operations can be performed
    ");
        printf("1. insert student information             input I
    ");
        printf("2. delete student information                    input D
    ");
        printf("3. inquiry student information                   input S
    ");
        printf("4. exit student information managent system      input Q
    ");
    }
    
    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
    ", mysql_errno(&mysql), mysql_error(&mysql));
            exit(-1);
        }
        mysql_set_character_set(&mysql, "utf8");
        while(1) {
            
            printf("please input student information
    ");
            printf("student_id: ");
            fflush(stdin);
            scanf("%s",id);
    //        fgets(id, 10, stdin);
            printf("
    name: ");
            scanf("%s",name);
    //        fgets(name, 20, stdin);
    
            printf("
    sex: ");
            scanf("%s",sex);
    
            printf("
    birth: ");
            scanf("%s",birth);
    //        fgets(birth, 15, stdin);
    
            printf("
    politily: ");
            scanf("%s",potility);
    //        fgets(potility, 20, stdin);
    
            printf("
    please input y or n or q
    ");
    //        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("
    %s",sql);
    
                if(mysql_query(&mysql, sql)){
                    printf("
    insert failed: %d--%s
    ", mysql_errno(&mysql), mysql_error(&mysql));
                    continue;
                }
                printf("
    insert sucess
    ");
    
                printf("you want exit? input q
    ");
                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
    ", 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
    ");
            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("
    student name: ");
                scanf("%s",name);
                //fgets(name, 20, stdin);
                sprintf(sql, "delete from students where name="%s";", name);            
            }else if(k == 'q')
                break;
            printf("
    you are sure? y or n or q
    ");
            getchar();
            k = fgetc(stdin);
            if(k == 'n')
                continue;
            else if(k == 'y'){
                if(mysql_query(&mysql, sql)){
                    printf("delete failed: %d--%s
    ", mysql_errno(&mysql), mysql_error(&mysql));
                    continue;
                }
                printf("delete sucess
    ");    
            }
            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
    ", 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
    ");
            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
    ", sql);
            }else if(k == 'n') {
                printf("
    student name: ");
                scanf("%s", name);
                //fgets(name, 20, stdin);
                sprintf(sql, "select * from students where name="%s";", name);
                printf("sql request:%s
    ", sql);
            }else if(k == 'q')
                break;
            printf("
    you are sure? y or n or q
    ");
            getchar();
            k = fgetc(stdin);
            if(k == 'n')
                continue;
            else if(k == 'y'){
                if(mysql_query(&mysql, sql)){
                    printf("delete failed: %d--%s
    ", 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("
    ");
                        }
                        printf("
    ");
                    }
    
                }else{
                    printf("no data found
    ");
                    continue;
                }
                
                printf("inquiry sucess
    ");    
            }
            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退出整个系统

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

  • 相关阅读:
    3delight and useBackground
    Maya 闪电
    jcCut1.1 在Maya里实现切割物体
    jcFeather 2.3.0 Demo
    javascript深度克隆的方法
    前端调用本地摄像头实现拍照(vue) Allen
    《暗时间》 读书笔记
    阅读笔记3流浪动物救助实践困境与路径优化
    阅读笔记1濒危动物网页的设计构思
    阅读笔记2电子宠物系统设计
  • 原文地址:https://www.cnblogs.com/qigaohua/p/5879498.html
Copyright © 2011-2022 走看看