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. Is it possible to define query globally
QtWS25 Last Chance

Is it possible to define query globally

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.7k Views
  • 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.
  • T Offline
    T Offline
    trupti
    wrote on last edited by
    #1

    Hello I am new in Qt Development.
    Can anyone plz suggest how to define a query globally??
    Also is it possible to use query in header file??

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

      Hi and welcome to devnet,

      You'll have to be more specific, what query are you talking about ? What do you want to program ?

      Did you took a look at Qt's documentation/tutorials/examples and demos ?

      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
      • T Offline
        T Offline
        trupti
        wrote on last edited by
        #3

        This is our sample code for login which we have made.
        Here in header file we have made function for opening and closing database connection and that function we have used in the cpp file.
        Now our issue is that we want to know that is it possible to write a function which has query like "select * from tablename" in header file and use this function in cpp file.?

        login.h

        namespace Ui {
        class login;
        }

        class login : public QMainWindow
        {
        Q_OBJECT

        public:
        QSqlDatabase mydb;

        void connClose()
        {
          mydb.close();
          mydb.removeDatabase(QSqlDatabase::defaultConnection);
        }
        bool connOpen()
        {
        
        mydb=QSqlDatabase::addDatabase("QSQLITE");
        mydb.setDatabaseName("/home/ubuntu/db1.db");
        
        if(!mydb.open())
        {
          qDebug()<<("Not Connected....");
          return false;
           }
        
        else
        {
          qDebug()<<("Connected....");
          return true;
          }
        }
        

        login.cpp

        void login::on_pushButton_clicked()
        {
        QString username,password;
        username=ui->lineEdit_uname->text();
        password=ui->lineEdit_pass->text();

        if(!connOpen())
        {
        qDebug()<<"Failed to open the database";
        return;
        }

        connOpen();
        QSqlQuery qry;
        qry.prepare("select * from login where username='"+username+"' and password='"+password+"'");

        if(qry.exec())
        {
        int count=0;
        while(qry.next())
        {
        count++;

           }
           if(count==1){
               ui->label->setText("username and password is correct");
           if(count<1)
               ui->label->setText("username and password is incorrect");
           if(count>1)
               ui->label->setText("duplicate username and password");
        

        }
        }

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          Is it possible to define query globally
          Yes .it is possible.

          [quote author="trupti" date="1410952271"]Hello I am new in Qt Development.
          Can anyone plz suggest how to define a query globally??
          Also is it possible to use query in header file??[/quote]

          [quote author="trupti" date="1411020931"]This is our sample code for login which we have made.
          Here in header file we have made function for opening and closing database connection and that function we have used in the cpp file.
          Now our issue is that we want to know that is it possible to write a function which has query like "select * from tablename" in header file and use this function in cpp file.?
          [/quote]
          you can take reference from this link..
          http://www.cplusplus.com/forum/beginner/16886/
          [quote author="trupti" date="1411020931"]
          @
          login.h

          namespace Ui {
          class login;
          }

          class login : public QMainWindow
          {
          Q_OBJECT

          public:
          QSqlDatabase mydb;

          void connClose()
          {
            mydb.close();
            mydb.removeDatabase(QSqlDatabase::defaultConnection);
          }
          bool connOpen()
          {
          
          mydb=QSqlDatabase::addDatabase("QSQLITE");
          mydb.setDatabaseName("/home/ubuntu/db1.db");
          
          if(!mydb.open())
          {
            qDebug()<<("Not Connected....");
            return false;
             }
          
          else
          {
            qDebug()<<("Connected....");
            return true;
            }
          }
          

          login.cpp

          void login::on_pushButton_clicked()
          {
          QString username,password;
          username=ui->lineEdit_uname->text();
          password=ui->lineEdit_pass->text();

          if(!connOpen())
          {
          qDebug()<<"Failed to open the database";
          return;
          }

          connOpen();
          QSqlQuery qry;
          qry.prepare("select * from login where username='"+username+"' and password='"+password+"'");

          if(qry.exec())
          {
          int count=0;
          while(qry.next())
          {
          count++;

             }
             if(count==1&#41;{
                 ui->label->setText("username and password is correct");
             if(count<1)
                 ui->label->setText("username and password is incorrect");
             if(count>1)
                 ui->label->setText("duplicate username and password");
          

          }
          }
          @
          [/quote]

          i see there is no error in you code;

          hope it helps.

          Be Cute

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

            @ trupti based on your questions, it looks like you are new to C++ and Qt. I'd recommend getting a good book on C++ first before going further

            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
            • T Offline
              T Offline
              trupti
              wrote on last edited by
              #6

              Thnk u all....

              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