我正在尝试从qsql数据库检索数据

llew8vvj  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(352)

但它不能从数据库中获取数据。我不想检索表中的数据,而是想在特定的行编辑中显示数据。错误是什么?有什么修改吗?
这是我正在使用的代码:

void Userdetails::on_pushButton_4_clicked()
{
    delete ui;

    // database connection
    database = QSqlDatabase::addDatabase("QMYSQL");
    database.setHostName("localhost");
    database.setUserName("root");
    database.setPassword("");
    database.setDatabaseName("electricity");

    if(database.open()) {
         QSqlQuery qry;

         QSqlQuery query(QSqlDatabase::database("MyConnect"));

         query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"));

         if(query.exec()) {
             query.exec();

             while(query.next()) {
                 ui ->dislayaccountnumber ->setText(query.value(0).toString());
                 ui ->displayname ->setText(query.value(3).toString());
                 ui ->displayaddress ->setText(query.value(4).toString());
                 ui ->displattelephoneno ->setText(query.value(5).toString());
                 // ui ->displayamountoebill ->setText(query.value(6).toString());
             }
         } else {
               QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
         }
    } else {
         QMessageBox::information(this, "Database not open", "Not opened successfully");
    }

    database.close();
}
8hhllhi2

8hhllhi21#

该代码有四个主要问题:
您已删除 ui 在代码的开头。因此,第一行调用 ui-> 会使程序崩溃。
您的查询定义不正确。另外,您为连接选择名称的方式也不正确。
您已经执行了两次查询(一次就足够了)。
您没有为绑定值 username 以及 password .
请使用以下选项:

void Userdetails::on_pushButton_4_clicked() {

    {
        // database connection
        QSqlDatabase database;
        database = QSqlDatabase::addDatabase("QMYSQL","MyConnect");
        database.setHostName("localhost");
        database.setUserName("root");
        database.setPassword("");
        database.setDatabaseName("electricity");

        if(database.open()) {

            QSqlQuery query(database);

            if (query.prepare(QString("SELECT accno, fullname, address, telephone FROM user_reg_elec WHERE username = :username AND password = :password"))) {

                //Add bindings
                query.bindValue(":username","your user name");
                query.bindValue(":password","your password");
                if(query.exec()) {

                    while(query.next()) {
                        ui ->dislayaccountnumber ->setText(query.value(0).toString());
                        ui ->displayname ->setText(query.value(1).toString());
                        ui ->displayaddress ->setText(query.value(2).toString());
                        ui ->displattelephoneno ->setText(query.value(3).toString());
                        // ui ->displayamountoebill ->setText(query.value(4).toString());
                    }
                } else {
                    qDebug() << "Query did not execute due to: " << query.lastError().text();
                    QMessageBox::information(this, "Query did not execute", "Not successful executing the query");
                }
            } else {
                qDebug() << "Query not prepared due to the following error: " << query.lastError().text();
            }
        } else {
            qDebug() << "Database not opened due to: " << database.lastError().text();
            QMessageBox::information(this, "Database not open", "Not opened successfully");
        }

        database.close();
    }

    QSqlDatabase::removeDatabase("MyConnect");
}

请添加 #include <QSqlError> 如果尚未包含此库,请转到顶部。
只对提问者说:
要在新窗口中显示详细信息,从数据库中获取目标用户的信息后,可以创建一个新对话框(窗口),并在其中显示结果,如下所示:

//Create a new dialog
    QDialog *dialog = new QDialog;

    //Add some elements to the dialog
    QLabel *accountNumber = new QLabel("Account number: " + query.value(0).toString());
    QLabel *name = new QLabel("Name: " + query.value(1).toString());
    QLabel *address = new QLabel("Address: " + query.value(2).toString());
    QLabel *phoneNumber = new QLabel("Phone number: " + query.value(3).toString());
    QVBoxLayout *lay = new QVBoxLayout;
    lay->addWidget(accountNumber);
    lay->addWidget(name);
    lay->addWidget(address);
    lay->addWidget(phoneNumber);
    dialog->setLayout(lay);

    //Show the dialog
    dialog->open();

相关问题