提问者:小点点

无法从SQLite数据库检索数据&在QT GUI上显示


我试图创建一个登录系统,如果一个人登录,它应该在登录GUI后面的Qt GUI上显示他们的名字。

我试过使用下面的代码,但是标签是空白的,所以我假设问题来自数据库。 我们将非常感谢您的帮助,谢谢。

#include "TaxiCoZa.h"

TaxiCoZa::TaxiCoZa(QWidget *parent):QWidget(parent) {   
    ui.setupUi(this);
    Connection x;
    isConnected(x)
};

void TaxiCoZa::on_loginButton_clicked() {
    emailInput = ui.emailEdit1->text();
    passwordInput = ui.passwordEdit1->text();
    Connection y;
    QSqlQuery query;
    
    query.prepare("SELECT * FROM riders WHERE email='" + emailInput + "' and password='" + passwordInput + "'");
    
    if (query.exec()) {
        int count = 0;
        
        while (query.next()) {
            count++;
        }
        // scenario for the correct password
        if (count == 1) {
            QString name;
            
            QSqlQuery query2;
            
            query2.prepare("SELECT (name) FROM riders WHERE email='" + emailInput + "' and password='" + passwordInput + "'");
            query2.exec();
            
            while (query.next())
            {
                name = query2.value(0).toString();
                break;
            }
            
            y.connClose();
            this->hide();
            Book b;
            b.setGreetingLabel("Hello, " + name);
            b.exec();   
        }
    }
}

共2个答案

匿名用户

您的query.prepare(“select.。。”)似乎没有正确编写。 试试这个;

query.prepare("SELECT * FROM riders WHERE email= 'emailInput' and password='passwordInput'");

另外,在第二个查询上做同样的更改。 请遵循该格式。

匿名用户

您确定数据库已连接到Qt吗?

如果是,那么尝试下面的行格式并qDebug输出。

query.prepare("SELECT * FROM riders WHERE email= :email and password = :pass");
query.bindValue(":email", emailInput);
query.bindValue(":pass", passwordInput);