C++登录&注册(连接数据库)

一、sql.h
#ifndef SQL_H
#define SQL_H
#include <mysql.h>
#include <iostream>

typedef struct Data {
    std::string username;
    std::string password;
}Data;

class Sql {
public:
    Sql();  //初始化
    ~Sql();  //释放空间
    bool connect();  //连接数据库
    bool login();  //登录
    bool re();  //注册
    bool correct();  //修改密码
    bool logout();  //注销账户
private:
    MYSQL mysql;
    MYSQL_RES* result;
    MYSQL_ROW sql_row;
    Data data;
};

#endif  //sql.h
二、sql.cpp
#include "sql.h"
#include <string>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
using std::cin;

Sql::Sql() {
    /*初始化*/
    mysql_init(&mysql);
    result = nullptr;
    sql_row = nullptr;
    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "GBK");
}

Sql::~Sql() {
    /*释放空间、断开连接*/
    mysql_free_result(result);
    mysql_close(&mysql);
    cout << "数据库已断开!" << endl;
}

bool Sql::connect() {
    /*连接数据库*/
    if (mysql_real_connect(&mysql, "localhost", "root", "root", "db_study", 3306, NULL, 0)) {
        cout << "数据库连接成功!" << endl;
        return true;
    }
    else {
        cout << "数据库连接失败!" << endl;
        cout << "error: " << mysql_error(&mysql) << endl;
        return false;
    }
}

bool Sql::login() {
    if (!mysql_query(&mysql, "select * from name_password")) {
        result = mysql_store_result(&mysql);  //得到结果集
        cout << "结果集数量为:" << mysql_num_rows(result) << endl;
        if (result) {
            /*检查用户名*/
            cout << "请输入您的用户名:";
            string name;
            cin >> name;
            cin.ignore();
            while (sql_row = mysql_fetch_row(result)) {
                if (sql_row[0] == name) {
                    /*用户名存在检查密码*/
                    cout << "请输入您的密码:";
                    string password;
                    getline(cin, password);
                    if (sql_row[1] == password) return true;
                    else { cout << "密码错误!" << endl; return false; }
                }
            }
        }
    }
    /*用户名不存在*/
    cout << "用户名不存在!" << endl;
    return false;
}

bool Sql::re() {
    string name;
    if (!mysql_query(&mysql, "select * from name_password")) {
        result = mysql_store_result(&mysql);
        cout << "结果集数量为:" << mysql_num_rows(result) << endl;
        if (result) {
            /*检查用户名*/
            cout << "请设置您的用户名:";
            cin >> name;
            cin.ignore();
            while (sql_row = mysql_fetch_row(result)) {
                if (sql_row[0] == name) { cout << "用户名重复!" << endl; return false; }
            }
        }
    }
    /*检验密码*/
    while (1) {
        cout << "请设置密码:";
        string password;
        getline(cin, password);

        cout << "请再次输入密码:";
        string check_password;
        getline(cin, check_password);

        if (password == check_password) {
            /*存入用户名和密码*/
            std::stringstream oss;
            oss << "insert into name_password(name,password) values('" << name << "','" << password << "');";
            string temp = oss.str();
            const char* query = temp.data();
            if (!mysql_query(&mysql, query)) return true;
            else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
            break;
        }
        else cout << "密码不一致!" << endl;
    }
    return true;
}

bool Sql::correct() {
    string name;
    if (!mysql_query(&mysql, "select * from name_password")) {
        result = mysql_store_result(&mysql);
        cout << "结果集数量为:" << mysql_num_rows(result) << endl;
        if (result) {
            /*检查用户名*/
            cout << "请输入您的用户名:";
            cin >> name;
            cin.ignore();
            while (sql_row = mysql_fetch_row(result)) {
                if (sql_row[0] == name) {
                    /*用户名存在*/
                    string password;
                    string check_password;
                    while (1) {
                        cout << "请设置新密码:";
                        getline(cin, password);
                        cout << "请重新输入密码:";
                        getline(cin, check_password);
                        if (password == check_password) {
                            /*密码检查一致*/
                            std::stringstream oss;
                            oss << "update name_password set password = '" << password << "' where name = '" << name << "';";
                            string temp = oss.str();
                            const char* query = temp.data();
                            if (!mysql_query(&mysql, query)) return true;
                            else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
                        }
                        else
                            cout << "密码不一致!" << endl;
                    }
                }
            }
        }
    }
    /*用户名不存在*/
    cout << "用户名不存在!" << endl;
    return false;
}

bool Sql::logout() {
    string name;
    if (!mysql_query(&mysql, "select * from name_password")) {
        result = mysql_store_result(&mysql);
        cout << "结果集数量为:" << mysql_num_rows(result) << endl;
        if (result) {
            cout << "请输入您要注销的用户名:";
            cin >> name;
            while (sql_row = mysql_fetch_row(result)) {
                if (sql_row[0] == name) {
                    /*用户名存在*/
                    std::stringstream oss;
                    oss << "delete from name_password where name = '" << name << "';";
                    string temp = oss.str();
                    const char* query = temp.data();
                    if (!mysql_query(&mysql, query)) {
                        /*删除成功,重新排序*/
                        if (!mysql_query(&mysql, "alter table name_password auto_increment = 1;")) {
                            cout << "重新排序成功!" << endl;
                        }
                        else { cout << "重新排序失败!" << endl; cout << "error reason: " << mysql_error(&mysql) << endl; }
                        return true;
                    }
                    else { cout << "error reason: " << mysql_error(&mysql) << endl; return false; }
                }
            }
        }
    }
    /*用户名不存在*/
    cout << "用户名不存在!" << endl;
    return false;
}
三、main.cpp
#include "sql.h"
using std::cout;
using std::endl;
using std::cin;

int main() {
    Sql sql;  //对象
    if (sql.connect()) {
        while (1) {
            cout << "****************" << endl;
            cout << "*  1.登录      *" << endl;
            cout << "*  2.注册      *" << endl;
            cout << "*  3.修改密码  *" << endl;
            cout << "*  4.注销账户  *" << endl;
            cout << "****************" << endl;
            int order;
            cout << "请输入您的指令:";
            cin >> order;
            switch (order) {
            case 1: 
                if (sql.login()) {
                    cout << "登录成功!" << endl;
                    /*登录成功后的界面*/


                }
                else cout << "登录失败!" << endl;
                cout << endl; 
                break;
            case 2: 
                if (sql.re()) cout << "注册成功!" << endl;
                else cout << "注册失败!" << endl;
                cout << endl; 
                break;
            case 3:
                if (sql.correct()) cout << "修改成功!" << endl;
                else cout << "修改失败!" << endl;
                cout << endl;
                break;
            case 4:
                if (sql.logout()) cout << "账户注销成功!" << endl;
                else cout << "账户注销失败!" << endl;
                cout << endl;
                break;
            default: cout << "输入的指令不存在!" << endl << endl; break;
            }
        }
    }
    else return 1;

    return 0;
}
四、配置:
  1. 包含目录:MYSQL\include

  1. 库目录:MYSQL\lib

  1. 调试环境:PATH=...\MYSQL\bin

  1. 链接器->输入->附加依赖项:libmysql.lib

  1. 将libmysql.dll添加到项目文件夹中

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

❀云坠入雾里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值