Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. QMAKE
Qt 6.11 is out! See what's new in the release blog

QMAKE

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
27 Posts 6 Posters 4.3k 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.
  • SGaistS SGaist

    You are storing a local QSqlDatabase object and are recreating a new connection that is replacing the one stored in the variable.

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

    @SGaist So how can it be solved?

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

      First thing, as the QSqlDatabase documentation explains: do not do that and if you remove the database connection, follow the documentation of the removeDatabase method..

      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

        First thing, as the QSqlDatabase documentation explains: do not do that and if you remove the database connection, follow the documentation of the removeDatabase method..

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

        @SGaist

        Thanks,I gone through that documention,but still not clear,I uninstalled the mysql server which was connected to the app at first,but after trying to connect for second ,am facing that error.
        The first connection used the default connection name. Am confused on how to desconnect the database with a default name.I have seen this function QSqldatabase::connectionName() but on how to use it am not sure.Please any help will be appreciated.

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

          How are you using QSqlDatabase in your code ?

          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

            How are you using QSqlDatabase in your code ?

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

            @SGaist
            Here is the code:

            QT       += core gui sql printsupport
            
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            CONFIG += c++11
            
            

            .h file

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include<QtSql>
            #include<QSqlDatabase>
            #include<QMessageBox>
            #include<mywork.h>
            //#include<payfee.h>
            
            QT_BEGIN_NAMESPACE
            namespace Ui { class MainWindow; }
            QT_END_NAMESPACE
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
                void closedb();
            
            
            private slots:
                void on_LOGIN_clicked();
            
                void on_REGISTER_clicked();
            
                void on_LOGIN_2_clicked();
            
            private:
                Ui::MainWindow *ui;
            QSqlDatabase database;
            
            
            };
            #endif // MAINWINDOW_H
            
            

            d.cpp file

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                ui->USERNAME_REG->setPlaceholderText("ENTER USERNAME");
                ui->E_MAIL->setPlaceholderText("ENTER YOUR E-MAIL ADDRESS");
                ui->PASSWORD_REG->setPlaceholderText("ENTER YOUR PASSWORD");
                ui->PHONE->setPlaceholderText("ENTER PHONE NUMBER");
                ui->USERNAME_LOGIN->setPlaceholderText("ENTER USERNAME");
                ui->PASSWORD_LOGIN->setPlaceholderText("ENTER PASSWORD");
            
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            
            void MainWindow::on_LOGIN_clicked()
            {
            
            QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
            db.setHostName("127.0.0.1");
            db.setUserName("root");
            db.setPassword("");
            db.setDatabaseName("mysql");
            
            if(db.open())  //database connections
             {
             QMessageBox::information(this,"connection","SYSTEM READY\nYOU CAN NOW REGISTER OR LOGIN!");
            
            }
            else {
            QMessageBox::information(this,"not connected","SYSTEM NOT CONNECTED");
            }
            
            }
            
            
            
            
            void MainWindow::on_REGISTER_clicked()
            {
                QString username = ui->USERNAME_REG->text();
                QString email=ui->E_MAIL->text();
                QString password =ui->PASSWORD_REG->text();
                QString phone =ui->PHONE->text();
            
                if(username=="" || email==""|| password==""||phone=="")
                {
            
                  QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
                }
                else
                  {//checking to avoid duplication of data
                    QSqlQuery query;
                    query.prepare("SELECT USERNAME,EMAIL,PHONE FROM users WHERE USERNAME=:USERNAME AND EMAIL=:EMAIL AND PHONE=:PHONE");
            
                       query.bindValue(":USERNAME",username);
                        query.bindValue(":EMAIL",email);
                        query.bindValue(":PHONE",phone);
            
                     if(query.exec()){
            
                            //QMessageBox::warning(this,"warninng","query successful!");
                             }
                    if(query.size()>0){
            
                            QMessageBox::warning(this,"WARNING","CHECK YOUR NAME,E-MAIL OR PHONE NUMBER PLEASE\n"
                                                          "ONE OF THESE HAVE BEEN USED BY OTHER USERS ALREADY!");
            
                    }
                       // QSqlQuery qry;
                    else
                          {
                //running insert query
            
                QSqlQuery qry;
            
                qry.prepare("INSERT INTO users(USERNAME,EMAIL,PASSWORD,PHONE)"
                            "VALUES(:USERNAME, :EMAIL, :PASSWORD, :PHONE)");
            
                qry.bindValue(":USERNAME",username);
                qry.bindValue(":EMAIL",email);
                qry.bindValue(":PASSWORD",password);
                qry.bindValue(":PHONE",phone);
            
                if(qry.exec())
                    {
                   QMessageBox::information(this,"WELCOME","YOU HAVE SUCCESSFULLY REGISTERED!\nYOU NOW LOGIN!");
            
                  }
                else
               {
            
                   QMessageBox::warning(this,"WARNING!","PLEASE TURN ON THE SYSTEM!");
            
            }
            
            }
            
            }
            }
            void MainWindow::on_LOGIN_2_clicked()
            {
                QString username=ui->USERNAME_LOGIN->text();
                QString password=ui->PASSWORD_LOGIN->text();
               QSqlQuery query(QSqlDatabase::database("Myconnect"));
            
               query.prepare("SELECT USERNAME,PASSWORD FROM users WHERE USERNAME=:USERNAME AND PASSWORD=:PASSWORD");
            
                  query.bindValue(":USERNAME",username);
                   query.bindValue(":PASSWORD",password);
            
                if(!query.exec()){
            
                       //QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
                        }
               if(query.size()>0){
                 //window two
                 hide();
                 MYWORK work;
                 work.setModal(true);
                 work.exec();
            
                }
                else
                   {
                   QMessageBox::warning(this,"CHENJEZO!","NOT SUCCESSFUL!\n"
                                                         "TURN ON THE SYSTEM OR ENTER CORRECT DETAILS!");
            
               }
            
                 }
            
            jsulmJ 1 Reply Last reply
            0
            • GREYONG GREYON

              @SGaist
              Here is the code:

              QT       += core gui sql printsupport
              
              
              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
              
              CONFIG += c++11
              
              

              .h file

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              #include<QtSql>
              #include<QSqlDatabase>
              #include<QMessageBox>
              #include<mywork.h>
              //#include<payfee.h>
              
              QT_BEGIN_NAMESPACE
              namespace Ui { class MainWindow; }
              QT_END_NAMESPACE
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  MainWindow(QWidget *parent = nullptr);
                  ~MainWindow();
                  void closedb();
              
              
              private slots:
                  void on_LOGIN_clicked();
              
                  void on_REGISTER_clicked();
              
                  void on_LOGIN_2_clicked();
              
              private:
                  Ui::MainWindow *ui;
              QSqlDatabase database;
              
              
              };
              #endif // MAINWINDOW_H
              
              

              d.cpp file

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  ui->USERNAME_REG->setPlaceholderText("ENTER USERNAME");
                  ui->E_MAIL->setPlaceholderText("ENTER YOUR E-MAIL ADDRESS");
                  ui->PASSWORD_REG->setPlaceholderText("ENTER YOUR PASSWORD");
                  ui->PHONE->setPlaceholderText("ENTER PHONE NUMBER");
                  ui->USERNAME_LOGIN->setPlaceholderText("ENTER USERNAME");
                  ui->PASSWORD_LOGIN->setPlaceholderText("ENTER PASSWORD");
              
              
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              
              void MainWindow::on_LOGIN_clicked()
              {
              
              QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
              db.setHostName("127.0.0.1");
              db.setUserName("root");
              db.setPassword("");
              db.setDatabaseName("mysql");
              
              if(db.open())  //database connections
               {
               QMessageBox::information(this,"connection","SYSTEM READY\nYOU CAN NOW REGISTER OR LOGIN!");
              
              }
              else {
              QMessageBox::information(this,"not connected","SYSTEM NOT CONNECTED");
              }
              
              }
              
              
              
              
              void MainWindow::on_REGISTER_clicked()
              {
                  QString username = ui->USERNAME_REG->text();
                  QString email=ui->E_MAIL->text();
                  QString password =ui->PASSWORD_REG->text();
                  QString phone =ui->PHONE->text();
              
                  if(username=="" || email==""|| password==""||phone=="")
                  {
              
                    QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
                  }
                  else
                    {//checking to avoid duplication of data
                      QSqlQuery query;
                      query.prepare("SELECT USERNAME,EMAIL,PHONE FROM users WHERE USERNAME=:USERNAME AND EMAIL=:EMAIL AND PHONE=:PHONE");
              
                         query.bindValue(":USERNAME",username);
                          query.bindValue(":EMAIL",email);
                          query.bindValue(":PHONE",phone);
              
                       if(query.exec()){
              
                              //QMessageBox::warning(this,"warninng","query successful!");
                               }
                      if(query.size()>0){
              
                              QMessageBox::warning(this,"WARNING","CHECK YOUR NAME,E-MAIL OR PHONE NUMBER PLEASE\n"
                                                            "ONE OF THESE HAVE BEEN USED BY OTHER USERS ALREADY!");
              
                      }
                         // QSqlQuery qry;
                      else
                            {
                  //running insert query
              
                  QSqlQuery qry;
              
                  qry.prepare("INSERT INTO users(USERNAME,EMAIL,PASSWORD,PHONE)"
                              "VALUES(:USERNAME, :EMAIL, :PASSWORD, :PHONE)");
              
                  qry.bindValue(":USERNAME",username);
                  qry.bindValue(":EMAIL",email);
                  qry.bindValue(":PASSWORD",password);
                  qry.bindValue(":PHONE",phone);
              
                  if(qry.exec())
                      {
                     QMessageBox::information(this,"WELCOME","YOU HAVE SUCCESSFULLY REGISTERED!\nYOU NOW LOGIN!");
              
                    }
                  else
                 {
              
                     QMessageBox::warning(this,"WARNING!","PLEASE TURN ON THE SYSTEM!");
              
              }
              
              }
              
              }
              }
              void MainWindow::on_LOGIN_2_clicked()
              {
                  QString username=ui->USERNAME_LOGIN->text();
                  QString password=ui->PASSWORD_LOGIN->text();
                 QSqlQuery query(QSqlDatabase::database("Myconnect"));
              
                 query.prepare("SELECT USERNAME,PASSWORD FROM users WHERE USERNAME=:USERNAME AND PASSWORD=:PASSWORD");
              
                    query.bindValue(":USERNAME",username);
                     query.bindValue(":PASSWORD",password);
              
                  if(!query.exec()){
              
                         //QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!");
                          }
                 if(query.size()>0){
                   //window two
                   hide();
                   MYWORK work;
                   work.setModal(true);
                   work.exec();
              
                  }
                  else
                     {
                     QMessageBox::warning(this,"CHENJEZO!","NOT SUCCESSFUL!\n"
                                                           "TURN ON THE SYSTEM OR ENTER CORRECT DETAILS!");
              
                 }
              
                   }
              
              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #18

              @GREYON said in QMAKE:

              QSqlDatabase database;

              As suggested already remove that (you do not even use it in the code you posted).
              There is no need to keep QSqlDatabase instance (explained in the documentation).

              QSqlQuery query(QSqlDatabase::database("Myconnect")); - where do you actually add this database? Please read https://doc.qt.io/qt-5/qsqldatabase.html#database
              "If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned."
              "

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

              GREYONG 1 Reply Last reply
              2
              • jsulmJ jsulm

                @GREYON said in QMAKE:

                QSqlDatabase database;

                As suggested already remove that (you do not even use it in the code you posted).
                There is no need to keep QSqlDatabase instance (explained in the documentation).

                QSqlQuery query(QSqlDatabase::database("Myconnect")); - where do you actually add this database? Please read https://doc.qt.io/qt-5/qsqldatabase.html#database
                "If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned."
                "

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

                @jsulm
                Thanks for your help .The Qsqldatabase database has been used in a difference class when calling for the instance database.But the main problem is that I can't connect to the database any more .am always told qmsql drivers not loaded.old connection removed.My am is to remove that old
                connectionn so that I can connect to the database again.

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

                  But why do you need to remove the connection in the first place ? Can't you just close it, update the information and open it again ?

                  How exactly are you using QSqlDatabase ?

                  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

                    But why do you need to remove the connection in the first place ? Can't you just close it, update the information and open it again ?

                    How exactly are you using QSqlDatabase ?

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

                    @SGaist
                    I don't really want to remove the connection,its like it became unconnected when i unistalled qt5 .9.9 and installal qt 5.12.5, i tried to install qt 5.9.9 but it did not become connected again.

                    on how QSqlDatabase is being used,It was used to create an object database which is being called when i want to access the database .below is an example of how it has been used.

                    private slots:
                        void on_LOGIN_clicked();
                    
                        void on_REGISTER_clicked();
                    
                        void on_LOGIN_2_clicked();
                    
                    private:
                        Ui::MainWindow *ui;
                    QSqlDatabase database;
                    
                    

                    MUSIC, CLASS.

                    #include "music.h"
                    #include "ui_music.h"
                    
                    MUSIC::MUSIC(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::MUSIC)
                    {
                        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)");
                    }
                    
                    MUSIC::~MUSIC()
                    {
                        delete ui;
                    }
                    
                    void MUSIC::on_ENTER_FEE_clicked()
                    {
                        QString surname =ui->SURNAME->text();
                        QString lastname=ui->OTHERNAME->text();
                        QString clas= ui->CLASS->text();
                        QString amount=ui->AMOUNT->text();
                        QString grade =ui->GRADE->text();
                        QString date=ui->DATE->text();
                    
                        if(surname=="" || grade==""|| clas==""||amount==""||lastname==""||date=="")
                        {
                    
                          QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS");
                        }
                        else
                          {
                    
                     QSqlDatabase::database();
                     QSqlQuery query;
                     query.prepare("SELECT SURNAME,LASTNAME FROM music WHERE SURNAME=:surname AND LASTNAME=:lastname");
                    
                        query.bindValue(":surname",surname);
                         query.bindValue(":lastname",lastname);
                    
                      if(query.exec()){
                    
                             //QMessageBox::warning(this,"warninng","query successful!");
                              }
                     if(query.size()>0){
                    
                             QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!\nUSE INITIAL IF ITS A DIFFERENT NAME!");
                                    }
                    
                        // QSqlQuery qry;
                               else
                                  {
                        query.prepare("INSERT INTO music (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)"
                    
                                    "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT,  :DATE)");
                    
                        query.bindValue(":SURNAME",surname);
                        query.bindValue(":GRADE",grade);
                        query.bindValue(":CLASS",clas);
                        query.bindValue(":AMOUNT",amount);
                        query.bindValue(":LASTNAME",lastname);
                        query.bindValue(":DATE",date);
                    
                        if(query.exec())
                            {
                           QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!");
                    
                          }
                        else
                       {
                    
                           QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!");
                    
                      }
                       }
                    
                    }
                    }
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #22

                      Do not keep a QSqlDatabase member variable.
                      It's explained in the class documentation.

                      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
                        tmapp
                        wrote on last edited by
                        #23

                        Hi,

                        • your first error was due to : QMAKE_USE += mysql in mysql.pro
                          Comment it and everything will move to the next step
                        • your second error: was due to the qmysql driver (missing driver or not compatible with your version)
                        • it is Ok to use QT with wampp mysql ( i'm using it with xampp )
                        • remake the driver, and that will solve the issue of : QMYSQL driver not loaded
                        • follow this reply you will achieve the connection :: Tested Solution

                        good luck.

                        GREYONG 2 Replies Last reply
                        0
                        • T tmapp

                          Hi,

                          • your first error was due to : QMAKE_USE += mysql in mysql.pro
                            Comment it and everything will move to the next step
                          • your second error: was due to the qmysql driver (missing driver or not compatible with your version)
                          • it is Ok to use QT with wampp mysql ( i'm using it with xampp )
                          • remake the driver, and that will solve the issue of : QMYSQL driver not loaded
                          • follow this reply you will achieve the connection :: Tested Solution

                          good luck.

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

                          @tmapp
                          Okay thanks for your help,am just not clear on how to remake drivers .

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #25

                            @GREYON said in QMAKE:

                            am just not clear on how to remake drivers .

                            RTM: https://doc.qt.io/qt-5/sql-driver.html#building-the-drivers or search the forum.

                            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
                            2
                            • Christian EhrlicherC Christian Ehrlicher

                              @GREYON said in QMAKE:

                              am just not clear on how to remake drivers .

                              RTM: https://doc.qt.io/qt-5/sql-driver.html#building-the-drivers or search the forum.

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

                              @Christian-Ehrlicher
                              Okay thanks

                              1 Reply Last reply
                              0
                              • T tmapp

                                Hi,

                                • your first error was due to : QMAKE_USE += mysql in mysql.pro
                                  Comment it and everything will move to the next step
                                • your second error: was due to the qmysql driver (missing driver or not compatible with your version)
                                • it is Ok to use QT with wampp mysql ( i'm using it with xampp )
                                • remake the driver, and that will solve the issue of : QMYSQL driver not loaded
                                • follow this reply you will achieve the connection :: Tested Solution

                                good luck.

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

                                @tmapp said in QMAKE:
                                I cannot see this line below in my code

                                QMAKE_USE +=

                                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