qt-mysql数据库检查登录和密码登录

zbdgwd5y  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(376)

我想在qt里做一个应用,里面会有登录和注册系统,登录和密码都在mysql数据库里。我已经连接数据库到我的应用程序,但我不知道如何比较登录和密码中给出的“行编辑”这是从教程的代码,但它是教程的sqlite

void login::on_pushButton_sign_in_clicked()

QString login, password;
login = ui->lineEdit_login_login->text();
password = ui->lineEdit_password_login->text();

QSqlQuery qry;

if(qry.exec("select logins, passwords * from users where logins =  '" + login + "' and password = '" + password + "' " ))
{
    while(qry.next())
    {
        hide();
        mainwindow = new MainWindow(this);
        mainwindow->show();
    }

}

}

6ioyuze2

6ioyuze21#

正如在注解中提到的,您应该首先了解基本的sql查询。假设你有一个 users 表,其列如下: | userId | name | username | password |myUsername 以及 myPassword 从自定义应用程序中,sql查询可能如下所示: SELECT userID, name from users WHERE username = 'myUsername' AND password = 'myPassword' 现在,您可以在代码中使用它:

void login::on_pushButton_sign_in_clicked()
{
    QString username = ui->lineEdit_login_login->text();
    QString password = ui->lineEdit_password_login->text();

    QSqlQuery query;
    query.prepare("SELECT userID, name from users WHERE 
                    username = :username AND 
                    password = :password");
    query.bindValue(":username", username);
    query.bindValue(":password", password);
    if (query.exec())
    {
        if (query.size() > 0)
        {
            // You login a user here
            QString name = query.value(1).toString();
            qDebug() << name << "is logged in";
        }
        else
        {
            qDebug() << "Login failed. Invalid username or password.";
        }
    }
}

请注意,我假设它是为了学习的目的。建议存储密码哈希而不是纯文本。干杯!

相关问题