Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "No query Unable to fetch row". Problem of login function and database.
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 7.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jocelyn
    wrote on last edited by p3c0
    #1

    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");
        }
    
    }
    
    p3c0P jsulmJ 2 Replies Last reply
    0
    • J Jocelyn

      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");
          }
      
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      J 1 Reply Last reply
      0
      • p3c0P p3c0

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

        J Offline
        J Offline
        Jocelyn
        wrote on last edited by Jocelyn
        #3

        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...

        p3c0P 1 Reply Last reply
        0
        • J Jocelyn

          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...

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @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.

          157

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 2 Replies Last reply
            0
            • SGaistS SGaist

              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.

              J Offline
              J Offline
              Jocelyn
              wrote on last edited by Jocelyn
              #6

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

              }

              1 Reply Last reply
              0
              • O Offline
                O Offline
                Ocascante
                wrote on last edited by
                #7

                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(.......

                J 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • J Jocelyn

                    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");
                        }
                    
                    }
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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))"
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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.

                      J Offline
                      J Offline
                      Jocelyn
                      wrote on last edited by
                      #10

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

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

                      thanks a lot..

                      1 Reply Last reply
                      0
                      • O Ocascante

                        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(.......

                        J Offline
                        J Offline
                        Jocelyn
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved