Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to Validate username and password from postgreSQL database ?[SOLVED]
Forum Updated to NodeBB v4.3 + New Features

How to Validate username and password from postgreSQL database ?[SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 4.3k 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.
  • N Offline
    N Offline
    NandiniN
    wrote on 25 Nov 2013, 12:39 last edited by
    #1

    I have created database 'creatordb' and table name 'creator_tbl'

    uresname | password

    nandi | 1234
    nandini | 123

    in VS2008

    #include "create.h"
    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QtSql/QSqlQuery>
    #include <QtSql/QtSql>
    #include <string>

    Create::Create(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);
    }

    Create::~Create()
    {

    }
    void Create::on_pushButton_Clicked()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");

    db.setHostName("localhost");
    db.setDatabaseName("creatordb");
    db.setUserName("postgres");
    db.setPassword("pgadmin@123");
    db.setPort(5432);
    db.open();
    bool ok=db.open();
    if(ok!=true )
    {
    QSqlQueryModel model;
    model.setQuery("select * from creator_tbl ");
    QMessageBox Msgbox;
    Msgbox.setText(" Failed ");
    Msgbox.exec();
    }
    else

    {

                       QMessageBox Msgbox;
    

    Msgbox.setText(" connected ");
    Msgbox.exec();

          QSqlQueryModel model;
    

    model.setQuery("select username from creator_tbl");

    Msgbox.setText("Hi...!!! Welcome you had successfully logged in to QuickWorker Task Management");
    Msgbox.exec();
    newCreate=new NewCreate();
    this->hide();

    newCreate->show();
    }
    }

    If i change the database name ,something like 'creatordb' to 'creatordb1' it gives a messagebox of FAILED.This s how i come to kn it is connected to database.

    If i give the wrong username and password also "Hi...!!! Welcome you had successfully logged in to QuickWorker Task Management" messagebox is displayed.

    Please help me with the code to validate ursname and psswd. I mean if i enter anything other than what db contains it should throw me ERROR saying 'data not matched'!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Nov 2013, 21:12 last edited by
      #2

      Hi,

      Please enclose your code with coding tags, it will make it readable. You seem to call open on db twice and another strange thing, if open fails you create a model ?

      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
      • N Offline
        N Offline
        NandiniN
        wrote on 26 Nov 2013, 04:24 last edited by
        #3

        @#include “create.h”
        #include <QMessageBox>
        #include <QSqlDatabase>
        #include <QtSql/QSqlQuery>
        #include <QtSql/QtSql>
        #include <string>

        Create::Create(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
        {
        ui.setupUi(this);
        }

        Create::~Create()
        {}
        void Create::on_pushButton_Clicked()
        {
        QSqlDatabase db = QSqlDatabase::addDatabase(“QPSQL”);

        db.setHostName(“localhost”);
        db.setDatabaseName(“creatordb”);
        db.setUserName(“postgres”);
        db.setPassword(“pgadmin@123”);
        db.setPort(5432);
        db.open();
        bool ok=db.open();
        if(ok!=true )
        {
        QSqlQueryModel model;
        model.setQuery(“select * from creator_tbl “);
        QMessageBox Msgbox;
        Msgbox.setText(” Failed “);
        Msgbox.exec();
        }
        else
        {
        QMessageBox Msgbox;
        Msgbox.setText(” connected “);
        Msgbox.exec();
        QSqlQueryModel model;
        model.setQuery(“select username from creator_tbl”); Msgbox.setText(“Hi…!!! Welcome you had successfully logged in to QuickWorker Task Management”);

        Msgbox.exec();

        newCreate=new NewCreate();

        this->hide();

        newCreate->show();
        }
        }
        @

        On giving incorrect database name it executes
        @Msgbox.setText(” Failed “);@

        and on giving correct db name i.e 'creatordb' it executes

        @Msgbox.setText(“Hi…!!! Welcome you had successfully logged in to QuickWorker Task Management”);@

        so my connection to db is correct isn't it?

        Please help me with this- how to validate username and password by fetching the data from the table?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 26 Nov 2013, 05:08 last edited by
          #4

          Hi,

          First as SGaist said you are opening the database twice here. Its useless.
          @
          db.open();
          bool ok=db.open();
          @

          Also you can use QSqlQuery directly instead to execute your query where you will fetch the required data from db.
          So for eg. thequery may go like this,
          @
          QSqlQuery query(QSqlDatabase::database());
          QString q = "select id from creator_tbl where username='testuser'";
          bool success = query.exec(q); //if there is no match success == false

          query.next(); //if required
          QString id = query.value(0).toString(); // if required

          if(success)
          qDebug() << "User validated" << id;
          else
          qDebug() << "User not validated";
          @

          157

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NandiniN
            wrote on 26 Nov 2013, 08:01 last edited by
            #5

            Now i have this-
            @else

            {
            QString username=ui.lineEdit_4->text();
            QString password=ui.lineEdit_3->text();
            QMessageBox Msgbox;
            Msgbox.setText(" connected ");
            Msgbox.exec();

            QSqlQuery query(QSqlDatabase::database());
            QString q = "select username,password from creator_tbl where username='"+username+"' and password='"+password+"'";
            bool success = query.exec(q);

            if(success)
            {
            Msgbox.setText(" data in the table is matched");
            Msgbox.exec();
            newCreate=new NewCreate();
            this->hide();
            newCreate->show();//this is my 2nd frame
            }
            else
            {
            Msgbox.setText("data NOT matched");
            Msgbox.exec();
            }
            }@

            I want data to be matched implicitly, instead of this-
            @QString q = "select id from creator_tbl where username='testuser'";@

            Following my code ,in the "login" page on entering usrname and password(randomly)

            1. Tells me db is connected
              @Msgbox.setText(" connected ");@
              2.Data in table is matched
              @Msgbox.setText(" data in the table is matched");@

            Even though the data what i have entered in lineEdit_4->text()[usrname] and lineEdit_3[passwd]->text() are incorrect @if(success) @ is returning TRUE.

            ALL i want is to COMPARE with database table values !! wats wrong or wat am i missing here?

            //qDebug()<<" ----"; is not all working so i am using .setText("------");

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 26 Nov 2013, 08:29 last edited by
              #6

              Oops sorry.
              Try
              @
              query.exec(q);
              int s = query.size();
              bool success = (s<=0) ? false:true;
              @

              So if any results are returned "success" becomes true as size will return number of rows affected or -1 any problem.

              157

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NandiniN
                wrote on 26 Nov 2013, 09:22 last edited by
                #7

                Thanks a TON .its working : )

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 26 Nov 2013, 09:30 last edited by
                  #8

                  You're Welcome :) You can mark it as solved by editing and prepending [solved] to the question.

                  157

                  1 Reply Last reply
                  0

                  8/8

                  26 Nov 2013, 09:30

                  • Login

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