简体   繁体   English

将 MySQL 连接到 C++ 控制台应用程序和简单的登录系统

[英]Connecting MySQL to C++ Console Application and simple login system

I'd like to make simple login system in C++ Console Application with MySQL, and if login details are correct, it should execute my actual code.我想用 MySQL 在 C++ 控制台应用程序中制作简单的登录系统,如果登录详细信息正确,它应该执行我的实际代码。

How can that be done?怎么可能呢?

If possible, i'd also like to add so, when user login's for the first time, to store his HWID in database so no one else can login with same login credentials.如果可能的话,我还想补充一点,当用户第一次登录时,将他的 HWID 存储在数据库中,这样其他人就不能使用相同的登录凭据登录。

Thanks in advance, I am new to C++ programming and I don't really know what should I do:)在此先感谢,我是 C++ 编程的新手,我真的不知道该怎么办:)

Here's a simple example of a login system:这是登录系统的一个简单示例:

#include <iostream>
#include <vector>
#include <map>
#include <string>

using std::cout;
using std::cin;
using std::string;
using std::vector;

struct Login_Info
{
    string name;
    unsigned int  id;
};

int main()
{
    vector<Login_Info> user_database;
    string user_name;
    cout << "Enter User's name:";
    getline(cout, user_name);
    unsigned int user_id = 0U;
    cout << "\nEnter User's ID: ";
    cin >> user_id;
    Login_Info user_info;
    user_info.name = user_name;
    user_info.id   = user_id;
    // Insert the User's info into the database.
    user_database.push_back(user_info);

    return EXIT_SUCCESS;
}

Searching and comparing are left as an exercise for the OP or Reader.搜索和比较留给 OP 或 Reader 作为练习。

Edit 1: Index tables编辑 1:索引表
Let's move the info I/O into the struct , then focus on building the database.让我们将 info I/O 移动到struct中,然后专注于构建数据库。

struct Login_Info
{
    string       name;
    unsigned int id;
    void  input_from_console(std::ostream& console_output_stream,
                             std::istream& console_input_stream);
};

void Login_Info::input_from_console(std::ostream& console_stream)
{
    console_output_stream << "Enter User's name: ";
    getline(console_input_stream, name);
    if (name.empty())
    {
        id = 0;
    }
    else
    {
        console_output_stream << "\nEnter User ID: ";
        console_input_stream >> id;
    }
}

int main()
{
    vector<Login_Info> user_database;
    std::map<unsigned int, unsigned int> index_by_id;
    Login_Info         user_info;
    while (true)
    {
        user_info.input_from_console(cout, cin);
        if (user_info.name.empty())
        {
            break;
        }
        unsigned int vector_index = user_database.size();
        user_database.push_back(user_info);  
        index_by_id[user_info.id] = vector_index;
    }
    return EXIT_SUCCESS;
}

The above program builds a database using std::vector , and creates an index table using std::map .上述程序使用std::vector构建数据库,并使用std::map创建索引表。

The User's name with ID == 5, can be found by: ID == 5 的用户名可以通过以下方式找到:

Login_Info user_info;
const unsigned int vector_index = index_by_id[5];
user_info = database[vector_index];  
cout << "User #5 is: " << user_info.name << "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM