③新建user表,插入数据,编写登录程序测试

用户表:用户id,姓名,密码,部门,职位

建表以及插入数据的sql语句

CREATE TABLE User          
          (Uid   CHAR(8) PRIMARY KEY, /* 列级完整性约束条件*/                  
            Uname  CHAR(20) UNIQUE,     /* Sname取唯一值*/
            Upwd    CHAR(40),
            Udpt   CHAR(20),
            UM  CHAR(20)
           );

INSERT  INTO  User    VALUES ('12345601','张成民','123456789','部门一','科员'); 
INSERT  INTO  User    VALUES ('12345602','张民','12345678','部门一','科员'); 

对登录行为进行用户名与密码的匹配,即在数据库中查询id+密码,判断数据库返回是否有一行,若返回行数不为一则判断密码错误,需要跳转重新输入

若密码正确则登录成功并打印输出个人信息

代码实现login函数如下

int login()
{

    MYSQL* my = mysql_init(NULL);

    if (!mysql_real_connect(my, "122.112.198.128", "rui1220", "z159753Z", "rui1220", 3306, NULL, 0)) {
        printf("connect error !\n");
        mysql_close(my);
    }
    mysql_query(my, "SET NAMES 'GB2312' ");

    char Uid[100];
    char pw[100];
    char str2[100] = "select * from User where Uid='12345601';";
    char sql[100];

sign:

    printf("请输入用户名\n");
    scanf_s("%s", &Uid,10);
    printf("请输入密码\n");
    scanf_s("%s", &pw, 10);
    sprintf_s(sql, "select * from User where Uid='%s' and Upwd='%s';", Uid,pw);
    //printf("%s\n", sql);
    
    int res = mysql_query(my, sql);
    //printf("%d", res);
    MYSQL_RES* a = mysql_store_result(my);
    int rows = mysql_num_rows(a);//行数量
    int cols = mysql_num_fields(a);//列数量
    //printf("%d\n", rows);
    if (!rows)
    {
        printf("用户名或密码错误,请重新输入\n");
        goto sign;
    }
    printf("登陆成功\n");

    //打印个人信息
    MYSQL_ROW line;
    line = mysql_fetch_row(a);
    printf("欢迎使用公文传输系统\n");
    printf("姓名:%s\n", line[1]);
    printf("工号:%s\n", line[0]);
    printf("部门:%s\n", line[3]);
    printf("职位:%s\n", line[4]);
    
    mysql_close(my);
    return 0;
}

 

 

posted @ 2022-05-25 14:34  ewdbdvdv  阅读(281)  评论(0编辑  收藏  举报