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 Class
Forum Updated to NodeBB v4.3 + New Features

Qt SQL Class

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 5 Posters 2.8k 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.
  • K Kris Revi
    25 Jul 2022, 13:46

    @jsulm said in Qt SQL Class:

    No. There are also bad examples out there.

    but 90% off the examples all have it in a custom class :S how can ALL of them be bad

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 25 Jul 2022, 13:49 last edited by
    #21

    @Kris-Revi What examples do you have in mind?
    And did you read the documentation you where pointed to? There it is clearly stated...

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

    K 1 Reply Last reply 25 Jul 2022, 14:01
    1
    • J jsulm
      25 Jul 2022, 13:49

      @Kris-Revi What examples do you have in mind?
      And did you read the documentation you where pointed to? There it is clearly stated...

      K Offline
      K Offline
      Kris Revi
      wrote on 25 Jul 2022, 14:01 last edited by
      #22

      @jsulm said in Qt SQL Class:

      And did you read the documentation you where pointed to? There it is clearly stated...

      reading now! so for login i should use QSqlQueryModel since it is read-only and not QSqlQuery am i understanding that correctly?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 25 Jul 2022, 14:04 last edited by
        #23

        No, you should:

        • properly handle the QSqlDatabase object
        • properly check that all your queries are successful

        QSqlQuery will not modify your database unless you write such a statement.

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

        K 1 Reply Last reply 25 Jul 2022, 14:17
        0
        • S SGaist
          25 Jul 2022, 14:04

          No, you should:

          • properly handle the QSqlDatabase object
          • properly check that all your queries are successful

          QSqlQuery will not modify your database unless you write such a statement.

          K Offline
          K Offline
          Kris Revi
          wrote on 25 Jul 2022, 14:17 last edited by Kris Revi
          #24

          @SGaist said in Qt SQL Class:

          QSqlQuery will not modify your database unless you write such a statement.

          Sooo THIS works it throws error if it cannot find username and password and i get "logged in" when i provide the correct username and password... am i still doing ANYTHING wrong?

              connect(ui->pushButtonLogin, &QPushButton::clicked, [=]()
              
                  {        
              
                      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              
                      //db.setHostName("acidalia");
              
                      db.setDatabaseName("C:/crap/db.sqlite");
              
                      //db.setDatabaseName("customdb");
              
                      //db.setUserName("mojito");
              
                      //db.setPassword("J0a1m8");
              
                      bool ok = db.open();
              
                      if(ok)
              
                      {
              
                          qInfo() << "Database Connected!";
              
              
              
                          QSqlQuery query;
              
                          query.prepare(QString("SELECT * FROM users WHERE username = (:username) AND password = (:password) "));
              
                          query.bindValue(":username", ui->usernameText->text());
              
                          query.bindValue(":password", ui->passwordText->text());
              
                          query.exec();
              
              
              
                          if (query.next())
              
                          {
              
                              m_username = ui->usernameText->text();
              
                              m_password = ui->passwordText->text();
              
                              m_isloggedin = true;
              
                              ui->errorLabel->setVisible(false);
              
                              ui->stackedWidget->setCurrentIndex(1);
              
                              QString name = query.value(1).toString();
              
                              qDebug() << name << "is logged in";
              
                          }
              
                          else
              
                          {
              
                              m_username = "";
              
                              m_password = "";
              
                              m_isloggedin = false;
              
                              ui->errorLabel->setVisible(true);
              
                              ui->errorLabel->setText("Invalid username or password.");
              
                              qWarning() << "Query Error : " << query.lastError();
              
                          }
              
                      }
              
                  });
              
              }
          
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 25 Jul 2022, 14:58 last edited by
            #25

            Yes, you are recreating your connection every time this slot is called which is not needed. And you are likely getting a warning while running your application and clicking your pushButtonLogin button multiple times.

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

            K 1 Reply Last reply 25 Jul 2022, 15:29
            1
            • S SGaist
              25 Jul 2022, 14:58

              Yes, you are recreating your connection every time this slot is called which is not needed. And you are likely getting a warning while running your application and clicking your pushButtonLogin button multiple times.

              K Offline
              K Offline
              Kris Revi
              wrote on 25 Jul 2022, 15:29 last edited by Kris Revi
              #26

              @SGaist said in Qt SQL Class:

              Yes, you are recreating your connection every time this slot is called which is not needed. And you are likely getting a warning while running your application and clicking your pushButtonLogin button multiple times.

              moved QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); to the mainwindow.h file

              JonBJ 1 Reply Last reply 25 Jul 2022, 17:48
              0
              • K Kris Revi
                25 Jul 2022, 15:29

                @SGaist said in Qt SQL Class:

                Yes, you are recreating your connection every time this slot is called which is not needed. And you are likely getting a warning while running your application and clicking your pushButtonLogin button multiple times.

                moved QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); to the mainwindow.h file

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 25 Jul 2022, 17:48 last edited by JonB
                #27

                @Kris-Revi
                I'm afraid that is not a good idea! Assuming you mean it is now a member variable, that is what @SGaist/Qt are asking you not to do. Have another careful read through https://doc.qt.io/qt-6/qsqldatabase.html#details, particularly the red box:

                Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

                Do the addDatabase() and open stuff somewhere early in code, don't save the QSqlDatabase db.

                You don't need the member variable because you can use QSqlDatabase db = QSqlDatabase::database(); just as a local variable whenever you need to access the default connection.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 25 Jul 2022, 17:52 last edited by
                  #28

                  If you are only using the default connection, then there's no need to do anything special beside the setup since it's the connection that will be used when no others have been created.

                  No need to over-engineer that stuff.
                  The usual main reason to retrieve the default connection is for example when you want to change credentials or in your case change the path to the database file.

                  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

                  21/28

                  25 Jul 2022, 13:49

                  • Login

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