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. QT SQL DATABASE LOGIN
Qt 6.11 is out! See what's new in the release blog

QT SQL DATABASE LOGIN

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.2k Views 1 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.
  • GREYONG Offline
    GREYONG Offline
    GREYON
    wrote on last edited by
    #1

    hello, am new in application development.
    I have connected my application to sql database and am able to enter data into the database through the user interface. But I want to make sure that a user does not enter same type of data twice.I have tried to use the if and the else statement,but the problem is that the else is not working, what could be the problem? I will appreciate your help. below is the code:```
    #include "schoolfee.h"
    #include "ui_schoolfee.h"

    schoolFee::schoolFee(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::schoolFee)
    {
    ui->setupUi(this);
    ui->SURNAME->setPlaceholderText("ENTER SURNAME");
    ui->GRADE->setPlaceholderText("INTER GRADE, e.g 9");
    ui->CLASS->setPlaceholderText("ENTER CLASS e.g 9A");
    ui->AMOUNT->setPlaceholderText("ENTER AMOUNT e.g 100");
    ui->OTHERNAME->setPlaceholderText("ENTER OTHER NAME(S)");
    //ui->DATE->setPlaceholderText("e.g 01/01/2000");

    }

    schoolFee::~schoolFee()
    {
    delete ui;

    }

    void schoolFee::on_ENTER_FEE_clicked()
    {

    QString name =ui->SURNAME->text();
    QString grade =ui->GRADE->text();
    QString clas= ui->CLASS->text();
    QString amount=ui->AMOUNT->text();
    QString receiver=ui->OTHERNAME->text();
    QString date=ui->DATE->text();
    
    
    
    if(name=="" || grade==""|| clas==""||amount==""||receiver==""||date=="")
    {
    
      QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
    }
    else
      {
    

    QSqlDatabase::database();
    QSqlQuery query;
    query.prepare(QString("SELECT SURNAME,LASTNAME FROM payments WHERE SURNAME=:SURNAME AND LASTNAME=:LASTNAME"));

    query.bindValue(":SURNAME",name);
    query.bindValue(":LASTNAME",receiver);

    if(!query.exec()){

         QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
    

    }
    else
    {

    while(query.next())
    {
    QString nameFromDB=query.value(0).toString();
    QString receiverFromDB=query.value(1).toString();

      if(nameFromDB==name && receiverFromDB==receiver){
    
          QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!!!\n"
               " USE INITIAL IF ITS A DIFFERENT NAME");
                            break;
                }
           else
         {
     
    
    
        QSqlQuery qry;
    
    qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
    
    qry.bindValue(":SURNAME",name);
    qry.bindValue(":GRADE",grade);
    qry.bindValue(":CLASS",clas);
    qry.bindValue(":AMOUNT",amount);
    qry.bindValue(":LASTNAME",receiver);
    qry.bindValue(":DATE",date);
    
    if(qry.exec())
        {
       QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
    
      }
    else
    

    {

       QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
    

    }
    }
    }
    }
    }

    }

    JonBJ 2 Replies Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @GREYON Then please mark this topic as solved, thx.

      GREYONG Offline
      GREYONG Offline
      GREYON
      wrote on last edited by
      #17

      @Christian-Ehrlicher
      Okay , thanks for the information,but am new here,how can it mark as solved please?

      1 Reply Last reply
      0
      • GREYONG GREYON

        hello, am new in application development.
        I have connected my application to sql database and am able to enter data into the database through the user interface. But I want to make sure that a user does not enter same type of data twice.I have tried to use the if and the else statement,but the problem is that the else is not working, what could be the problem? I will appreciate your help. below is the code:```
        #include "schoolfee.h"
        #include "ui_schoolfee.h"

        schoolFee::schoolFee(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::schoolFee)
        {
        ui->setupUi(this);
        ui->SURNAME->setPlaceholderText("ENTER SURNAME");
        ui->GRADE->setPlaceholderText("INTER GRADE, e.g 9");
        ui->CLASS->setPlaceholderText("ENTER CLASS e.g 9A");
        ui->AMOUNT->setPlaceholderText("ENTER AMOUNT e.g 100");
        ui->OTHERNAME->setPlaceholderText("ENTER OTHER NAME(S)");
        //ui->DATE->setPlaceholderText("e.g 01/01/2000");

        }

        schoolFee::~schoolFee()
        {
        delete ui;

        }

        void schoolFee::on_ENTER_FEE_clicked()
        {

        QString name =ui->SURNAME->text();
        QString grade =ui->GRADE->text();
        QString clas= ui->CLASS->text();
        QString amount=ui->AMOUNT->text();
        QString receiver=ui->OTHERNAME->text();
        QString date=ui->DATE->text();
        
        
        
        if(name=="" || grade==""|| clas==""||amount==""||receiver==""||date=="")
        {
        
          QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
        }
        else
          {
        

        QSqlDatabase::database();
        QSqlQuery query;
        query.prepare(QString("SELECT SURNAME,LASTNAME FROM payments WHERE SURNAME=:SURNAME AND LASTNAME=:LASTNAME"));

        query.bindValue(":SURNAME",name);
        query.bindValue(":LASTNAME",receiver);

        if(!query.exec()){

             QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
        

        }
        else
        {

        while(query.next())
        {
        QString nameFromDB=query.value(0).toString();
        QString receiverFromDB=query.value(1).toString();

          if(nameFromDB==name && receiverFromDB==receiver){
        
              QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!!!\n"
                   " USE INITIAL IF ITS A DIFFERENT NAME");
                                break;
                    }
               else
             {
         
        
        
            QSqlQuery qry;
        
        qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                    "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
        
        qry.bindValue(":SURNAME",name);
        qry.bindValue(":GRADE",grade);
        qry.bindValue(":CLASS",clas);
        qry.bindValue(":AMOUNT",amount);
        qry.bindValue(":LASTNAME",receiver);
        qry.bindValue(":DATE",date);
        
        if(qry.exec())
            {
           QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
        
          }
        else
        

        {

           QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
        

        }
        }
        }
        }
        }

        }

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2
        This post is deleted!
        1 Reply Last reply
        0
        • GREYONG GREYON

          hello, am new in application development.
          I have connected my application to sql database and am able to enter data into the database through the user interface. But I want to make sure that a user does not enter same type of data twice.I have tried to use the if and the else statement,but the problem is that the else is not working, what could be the problem? I will appreciate your help. below is the code:```
          #include "schoolfee.h"
          #include "ui_schoolfee.h"

          schoolFee::schoolFee(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::schoolFee)
          {
          ui->setupUi(this);
          ui->SURNAME->setPlaceholderText("ENTER SURNAME");
          ui->GRADE->setPlaceholderText("INTER GRADE, e.g 9");
          ui->CLASS->setPlaceholderText("ENTER CLASS e.g 9A");
          ui->AMOUNT->setPlaceholderText("ENTER AMOUNT e.g 100");
          ui->OTHERNAME->setPlaceholderText("ENTER OTHER NAME(S)");
          //ui->DATE->setPlaceholderText("e.g 01/01/2000");

          }

          schoolFee::~schoolFee()
          {
          delete ui;

          }

          void schoolFee::on_ENTER_FEE_clicked()
          {

          QString name =ui->SURNAME->text();
          QString grade =ui->GRADE->text();
          QString clas= ui->CLASS->text();
          QString amount=ui->AMOUNT->text();
          QString receiver=ui->OTHERNAME->text();
          QString date=ui->DATE->text();
          
          
          
          if(name=="" || grade==""|| clas==""||amount==""||receiver==""||date=="")
          {
          
            QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
          }
          else
            {
          

          QSqlDatabase::database();
          QSqlQuery query;
          query.prepare(QString("SELECT SURNAME,LASTNAME FROM payments WHERE SURNAME=:SURNAME AND LASTNAME=:LASTNAME"));

          query.bindValue(":SURNAME",name);
          query.bindValue(":LASTNAME",receiver);

          if(!query.exec()){

               QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
          

          }
          else
          {

          while(query.next())
          {
          QString nameFromDB=query.value(0).toString();
          QString receiverFromDB=query.value(1).toString();

            if(nameFromDB==name && receiverFromDB==receiver){
          
                QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!!!\n"
                     " USE INITIAL IF ITS A DIFFERENT NAME");
                                  break;
                      }
                 else
               {
           
          
          
              QSqlQuery qry;
          
          qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                      "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
          
          qry.bindValue(":SURNAME",name);
          qry.bindValue(":GRADE",grade);
          qry.bindValue(":CLASS",clas);
          qry.bindValue(":AMOUNT",amount);
          qry.bindValue(":LASTNAME",receiver);
          qry.bindValue(":DATE",date);
          
          if(qry.exec())
              {
             QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
          
            }
          else
          

          {

             QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
          

          }
          }
          }
          }
          }

          }

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @GREYON said in QT SQL DATABASE LOGIN:

          the problem is that the else is not working, what could be the problem?

          Hi & welcome.

          Which else, and what does "not working" mean, what happens that should not?

          1 Reply Last reply
          0
          • GREYONG Offline
            GREYONG Offline
            GREYON
            wrote on last edited by
            #4

            This part:```
            else
            {

                QSqlQuery qry;
            
            qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                        "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
            
            qry.bindValue(":SURNAME",name);
            qry.bindValue(":GRADE",grade);
            qry.bindValue(":CLASS",clas);
            qry.bindValue(":AMOUNT",amount);
            qry.bindValue(":LASTNAME",receiver);
            qry.bindValue(":DATE",date);
            
            if(qry.exec())
                {
               QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
            
              }
            else
            

            {

               QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
            

            }
            }
            }
            }
            }

            JonBJ 1 Reply Last reply
            0
            • GREYONG Offline
              GREYONG Offline
              GREYON
              wrote on last edited by
              #5

              The main aim is prevent the user from entering the same **SURNAME and LASTNAME twice, those line of code I wanted them to be executed if the data entered does not match ant any of the data already in the data base .but instead its not responding

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

                Hi,

                That's the kind of constraints that should be done at the database level.
                Your database structure might also be too simple in the sense that if can be pretty easy to get duplicated data with small typos.

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

                GREYONG 1 Reply Last reply
                1
                • GREYONG GREYON

                  This part:```
                  else
                  {

                      QSqlQuery qry;
                  
                  qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                              "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
                  
                  qry.bindValue(":SURNAME",name);
                  qry.bindValue(":GRADE",grade);
                  qry.bindValue(":CLASS",clas);
                  qry.bindValue(":AMOUNT",amount);
                  qry.bindValue(":LASTNAME",receiver);
                  qry.bindValue(":DATE",date);
                  
                  if(qry.exec())
                      {
                     QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
                  
                    }
                  else
                  

                  {

                     QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
                  

                  }
                  }
                  }
                  }
                  }

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @GREYON
                  You should say what goes wrong? Do you get a compilation error? A runtime error? A message? If so, tell us what the message is. Something doesn't do what you expect? If so what, and how do you know? That's what's required for people to look at your issue!

                  qry.prepare("INSERT INTO payments (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                              "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
                  

                  Those two strings get joined. Look at them, and you will realize that will generate in the middle:

                  ... CLASS,AMOUNT,DATE)VALUES(:SURNAME,
                  

                  The VALUES touches the parenthesis to its left, which is different from the sample code at https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values. I don't know if that will be enough to error.

                  In any case, when a query goes wrong (if that's what's happening), use https://doc.qt.io/qt-5/qsqlquery.html#lastError to print out the text of the error.

                  1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

                    That's the kind of constraints that should be done at the database level.
                    Your database structure might also be too simple in the sense that if can be pretty easy to get duplicated data with small typos.

                    GREYONG Offline
                    GREYONG Offline
                    GREYON
                    wrote on last edited by
                    #8

                    @SGaist
                    Now what are you suggesting items of help?

                    JonBJ SGaistS 2 Replies Last reply
                    0
                    • GREYONG GREYON

                      @SGaist
                      Now what are you suggesting items of help?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @GREYON

                      In any case, when a query goes wrong (if that's what's happening), use https://doc.qt.io/qt-5/qsqlquery.html#lastError to print out the text of the error.

                      Have you done this yet?

                      GREYONG 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @GREYON

                        In any case, when a query goes wrong (if that's what's happening), use https://doc.qt.io/qt-5/qsqlquery.html#lastError to print out the text of the error.

                        Have you done this yet?

                        GREYONG Offline
                        GREYONG Offline
                        GREYON
                        wrote on last edited by
                        #10

                        @JonB
                        Not yet,thanks let me go through it

                        1 Reply Last reply
                        0
                        • GREYONG GREYON

                          @SGaist
                          Now what are you suggesting items of help?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @GREYON said in QT SQL DATABASE LOGIN:

                          @SGaist
                          Now what are you suggesting items of help?

                          What experience do you have with relational database design ?

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

                          GREYONG 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            @GREYON said in QT SQL DATABASE LOGIN:

                            @SGaist
                            Now what are you suggesting items of help?

                            What experience do you have with relational database design ?

                            GREYONG Offline
                            GREYONG Offline
                            GREYON
                            wrote on last edited by
                            #12

                            @SGaist
                            Not as experience as such,we let about it ,it was part of our course now we have a project.

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

                              Well, in that case you should start by learning a bit more about them before going further. At least take the time to go through Qt's SQL module documentation and examples.

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

                              GREYONG 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                Well, in that case you should start by learning a bit more about them before going further. At least take the time to go through Qt's SQL module documentation and examples.

                                GREYONG Offline
                                GREYONG Offline
                                GREYON
                                wrote on last edited by
                                #14

                                @SGaist
                                Yeah,I think so

                                GREYONG 1 Reply Last reply
                                1
                                • GREYONG GREYON

                                  @SGaist
                                  Yeah,I think so

                                  GREYONG Offline
                                  GREYONG Offline
                                  GREYON
                                  wrote on last edited by
                                  #15

                                  @GREYON
                                  Thanks after going through Qsql documentation,the problem has been resolved

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • GREYONG GREYON

                                    @GREYON
                                    Thanks after going through Qsql documentation,the problem has been resolved

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #16

                                    @GREYON Then please mark this topic as solved, thx.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    GREYONG 1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @GREYON Then please mark this topic as solved, thx.

                                      GREYONG Offline
                                      GREYONG Offline
                                      GREYON
                                      wrote on last edited by
                                      #17

                                      @Christian-Ehrlicher
                                      Okay , thanks for the information,but am new here,how can it mark as solved please?

                                      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