Skip to content
Advertisement

I’m trying to retrieve data from qSQL database

But it does not get the data from the database. I’m do not want to retrieve the data to the table and want to display data in particular line edit. What is the error? Is there any modification?

This is the Code that I’m using:

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();
}

Advertisement

Answer

This code has four main issues:

  1. You have deleted the ui at the beginning of your code. Hence, the first line that calls ui-> will crash the program.
  2. You have defined your query improperly. Also, your way to select a name for your connection is not right.
  3. You have executed your query twice (one is enough).
  4. You’ve not bind values for username and password.

Please use the following:

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");
}

Please add #include <QSqlError> to the top, if you have not already included this library.

Only to the questionary:

To show details in a new window, after you got the target user’s information from your database, you can create a new dialog (window) and show the results in it as follows:

    //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();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement