"No query Unable to fetch row". Problem of login function and database.



  • Below is my code..l face the problem of login into database (using sqlite database browser). Please help me to detect what's happening..

    #include "logininterface.h"
    #include "ui_logininterface.h"
    #include <QTime>
    #include <QDebug>
    #include <QtSql>
    #include <QMessageBox>
    #include <QApplication>
    #include <QSqlQuery>
    #include <QSqlError>
    #include <QMessageBox>
    #include <QSqlError>
    
    LoginInterface::LoginInterface(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::LoginInterface)
    {
        ui->setupUi(this);
    
        QPixmap pix("C:/Users/user/Documents/Qt/loginPic.gif");
        ui->picLabel->setPixmap(pix);
    
        QSqlDatabase dbSaya; 
    
        dbSaya.setDatabaseName("C:/Users/user/Documents/Qt/shop.db");
    
        dbSaya.open();
    }
    
    LoginInterface::~LoginInterface()
    {
        delete ui;
    }
    
    void LoginInterface::on_loginPushButton_clicked()
    {
        QString userId,passWord;
        userId=ui->userIdLineEdit->text();
        passWord=ui->passwordLineEdit->text();
    
        ui->userIdLineEdit->setText(userId);
        ui->passwordLineEdit->setText(passWord);
    
        QSqlQuery querySaya;
    
        querySaya.prepare("CREATE TABLE IF NOT EXITS Login(userId VARCHAR(12) UNIQUE PRIMARY KEY, passWord VARCHAR(20))");
    
        if (!querySaya.exec())
        {
           
    
            qDebug()<<querySaya.lastError().text();
        }
        else
            qDebug()<<"Table created";
    
        querySaya.prepare("INSERT INTO Login(userId, passWord) VALUES ('"+userId+"','"+passWord+"')");
    
    
        if(querySaya.exec())
        {
            int count = 0;
            while (querySaya.next())
            {
                count++;
            }
            if(count==1)
            {
                ui->label->setText("username and passWord is correct");
                this->hide();
        
            }
    
            if(count>1)
                ui->label->setText("Duplicate and passWord is correct");
            if(count<1)
                ui->label->setText("username and passWord is not correct");
        }
    
    }
    

  • Moderators

    Hi @Jocelyn
    As the error says, there's nothing to fetch after you insert into the database.



  • Hi @p3c0, nice to know you...

    1. How can l insert data into my database?
    2. Since this is login function, l would like to make my userID and password match with my stored data .. How should l code??
      **Do you need to see my main.cpp and others source code??
      **Actually l'm using sqlite database browser to store data....

    Thank s in advance...


  • Moderators

    @Jocelyn

    How can l insert data into my database?

    insert query and the code is fine.

    Since this is login function, l would like to make my userID and password match with my stored data .. How should l code??

    Usually the data which you need should already be present in the database. Then you can just use select query against some condition (password in this case) and then check the data after query.next() or size should work too.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's another thing missing: you're note telling QSqlDatabase what driver to use. If it's SQLite the correct initialization would be:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    

    You should also check the return value of open and at least print a warning statement with the error message. That would have helped you diagnose the problem more quickly.



  • @SGaist
    l've edited my code, but still unable to connect..

    #include "logininterface.h"
    #include "ui_logininterface.h"

    #include <QTime>
    #include <QDebug>
    #include <QtSql>
    #include <QMessageBox>
    #include <QApplication>
    #include <QSqlQuery>
    #include <QSqlError>
    #include <QMessageBox>
    #include <QSqlError>

    LoginInterface::LoginInterface(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginInterface)
    {
    ui->setupUi(this);

    QPixmap pix("C:/Users/user/Documents/ Qt/loginPic.gif");
    ui->picLabel->setPixmap(pix);
    
    QSqlDatabase dbSaya = QSqlDatabase::addDatabase("QSQLITE");
    
    dbSaya.setDatabaseName("C:/Users/user/ Qt/shop.db");
    
    if(!dbSaya.open())
        QMessageBox::critical(0, QObject::tr("Login failed to open..."),dbSaya.lastError().text());
    else
    {
    
        QMessageBox::information(0,"Login Interface","Welcome to this system,\nplease login before proceeding...\t");
    
    }
    

    }

    LoginInterface::~LoginInterface()
    {
    delete ui;
    }

    void LoginInterface::on_loginPushButton_clicked()
    {
    QString userId,passWord;
    userId=ui->userIdLineEdit->text();
    passWord=ui->passwordLineEdit->text();

    ui->userIdLineEdit->setText(userId);
    ui->passwordLineEdit->setText(passWord);
    
    
    QSqlQuery querySaya;
    
    querySaya.prepare("CREATE TABLE Login(userId VARCHAR(12)PRIMARY KEY, passWord VARCHAR(20))");
    
    if (!querySaya.exec())
    {
           qDebug()<<querySaya.lastError().text();
    }
    else
        qDebug()<<"Table created";
    
    
    querySaya.exec("SELECT * FROM Login where userId='"+userId+"' and passWord='"+passWord+"')");
    
    if(querySaya.next())
    {
        QMessageBox::warning(this,"Login successfully",querySaya.lastError().text());
    }
    else
    {
        ui->statusLabel->setText("[!] Invalid UserID and Password..\n  Please enter again! ");
        ui->userIdLineEdit->setText(userId);
        ui->passwordLineEdit->setText(passWord);
    }
    

    }



  • Hello,

    In your "proyect.pro" :
    QT += core gui sql

    In your "....cpp:

    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlQuery>

    In your function to open:

    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(databasename);
    if(db.open()) .....
    Hope this help!

    Qsqlquery query;
    query.exec(.......


  • Lifetime Qt Champion

    What errors are you getting ?

    Not that if you already have a Login table in your database, you can't create a second one of the same name.


  • Moderators

    @Jocelyn There is a typo:

    "CREATE TABLE IF NOT EXITS Login(userId VARCHAR(12) UNIQUE PRIMARY KEY, passWord VARCHAR(20))"
    

    it should be (EXISTS not EXITS):

    "CREATE TABLE IF NOT EXISTS Login(userId VARCHAR(12) UNIQUE PRIMARY KEY, passWord VARCHAR(20))"
    


  • @SGaist
    Thank you..haha
    l've added it in my main.cpp:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

    thanks a lot..



  • @Ocascante
    Hi..
    ok..l've checked it..those code are to ensure that my Qt links with database..
    thanks a lot..thanks
    =]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.