AlexAgla
AlexAgla

Reputation: 63

QSqlError("',"',"',) In QT widget app

I am making an ATM app but for some reason when the code is executed to display the users balance, it doesnt properly fetch the value from the database. it throws the error QSqlError(""'."",""').

class bankfn : public QObject{
public:

    int getCheq(QString e){
        QSqlDatabase myDB = QSqlDatabase::database("myConnection",true);
        QSqlQuery qry(myDB);

        qry.prepare("SELECT cheqBal FROM userRecords WHERE email = (:email)");
        qry.bindValue(":email",e);
        qDebug() << qry.lastError();
        return qry.exec();
    }

    int getSav(QString e){
        QSqlDatabase myDB = QSqlDatabase::database("myConnection",true);
        QSqlQuery qry(myDB);

        qry.prepare("SELECT savbal FROM userRecords WHERE email = (:email)");
        qry.bindValue(":email",e);
        qDebug() << qry.lastError();
        return qry.exec();
    }

    void setCheq(QString e);
    void setSav(QString e);
};


void maunMenu::setupLogin() //Set up login page for after user credential check
{
   ui->cheqbalL->setText("$"+bank.getCheq(sm.activeUser()));
   ui->savBalL->setText("$"+bank.getSav(sm.activeUser()));
}

Upvotes: 1

Views: 557

Answers (1)

Pavan Chandaka
Pavan Chandaka

Reputation: 12751

Actually it seems you are not getting any errors. You are trying to print a no error situation.

Your exec() function call returns a bool value.

If you want to track an error, Check for the bool value returned by exec() statement first.

The exec() function returns false, in case of query execution failure.

Should be somewhat as shown below:

int getCheq(QString e){
..
..
..
..
       bool error = qry.exec();

       //HANDLE THE ERROR IF THE EXEC RETURNS FALSE
       if(!error)       
         qDebug() << qry.lastError();

       return isErrored ;
}

More over your getCheq function returns , if the execution of query is success or not. It will not return the value you expect.

You have to return the value of query result.

Change the function return type to double

And you can return the value from query as shown below.

return qry.value(0).toDouble();

Note: Take care of NULL checks before returning values/accessing members.

Upvotes: 1

Related Questions