Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved SQLITE no query unable to fetch the row

    General and Desktop
    4
    10
    483
    Loading More Posts
    • 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.
    • A
      AliM93 last edited by

      Hi everybody! I have a code'structure like this :
      include
      login.h
      paginaprincipale.h
      sc_assistivo.h
      source
      login.cpp
      paginaprincipale.cpp
      sc_assistivo.cpp
      ui
      login.ui
      paginaprincipale.ui
      sc_assistivo.ui

      I save data on a db in sqlite ahd it work fine for the file login and paginaprincipale, but when i do the same for the sc_assistivo file i get the error no quey unable to fetch the row. And i did the same identical things for all the three files.
      Does anybody have an idea?
      i works with cmake, but the file cmakelist seems to be correct. Moreover when i check if the db is connected i've got positive answer. the trouble starts when i try to modify the db after clicking a push putton (which is declared as a private slot)
      ps. i don't add the database path every time, i just have done it once and then in other files i use
      QSqlDatabase mydb2 = QSqlDatabase::database();

      QSqlDatabase mydb3 = QSqlDatabase::database();

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Please show some code - we can't do anything with the filenames of your project...

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

        1 Reply Last reply Reply Quote 1
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          If you only have one database and use the default connection, you don't need to do that.

          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 Reply Quote 1
          • A
            AliM93 last edited by

            Screenshot from 2020-05-30 21-41-07.png
            Screenshot from 2020-05-30 21-41-44.png

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              As stated in the QSqlDatabase documentation, don't keep class member variable of that type.

              Beside that, in the constructor, you are shadowing that variable so the member variable isn't valid and if you use it later on, you'll be trying to access an invalid connection.

              Therefore: start by removing these QSqlDatabase member variables.

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

              A 1 Reply Last reply Reply Quote 2
              • A
                AliM93 @SGaist last edited by

                @SGaist sorry, i can't understand, which variable are you referring to? i try to remove QSqlDatabase member but i get that the db is not connected.

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @AliM93 last edited by

                  @AliM93
                  Hi
                  Yes the one in the .h file.
                  You dont use it in the code show as you have a local variable of same name.

                  Also in the code shown, i cant see where you tell it what data file to use ?

                  1 Reply Last reply Reply Quote 2
                  • A
                    AliM93 last edited by

                    i remove that but the i get the same error, i've created a connection with the database in another file, and then i use the same connection in this, so i don't need to use any path. the strange thing is that i also create a query to select name and last name in the database and shot in my window, and that works! but then, when i move into the check button function i can't do anything with db. so why in the ui(new Ui::sc:assistivo) i can read data and in the void function no? thank you all for taking the time

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @AliM93 last edited by mrjj

                      @AliM93

                      Ok, its hard to overview with not all code but can we just note the following rules.

                      1: dont store the database variable. Qt always keeps track of this for you.
                      So its perfectly fine to open it somewhere and then uses query some where else.

                      Only exception is if you want to use multiple databases at same file. As in different files.
                      Then we need to use names pr database and give to query.

                      2: If Sqllite dont find the file you ask it to open, It creates a new blank one and opens it
                      so it looks good but it has no data.

                      3: you must tell it a file or memory
                      db.setDatabaseName(":memory:");
                      or
                      db.setDatabaseName("c:/folder/test.db");

                      small example just make sure its clear.

                      bool createConnection()
                      {
                          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                          db.setDatabaseName(":memory:"); 
                          if (!db.open()) {
                              QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.",
                                                    QMessageBox::Cancel);
                              return false;
                          }
                          QSqlQuery query;
                          qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                                               "firstname varchar(20), lastname varchar(20), num int )");
                          query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')");
                          query.exec("insert into person values(102, 'Christine', 'Holand','2')");
                          query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
                          query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
                          query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
                          return true;
                      }
                      
                      // here we dont use the QSqlDatabase db  variable at all. we just use query
                      void queryToCsv()
                      {
                          QSqlQuery query;
                          query.prepare(" select * from person;");
                          QFile csvFile ("output.csv");
                          if (!csvFile.open(QFile::WriteOnly | QFile::Text)) {
                              qDebug("failed to open csv file");
                              return;
                          }
                          if (!query.exec()) {
                              qDebug("failed to run query");
                              return;
                          }
                          QTextStream outStream(&csvFile);
                          outStream.setCodec("UTF-8");
                          while (query.next()) {
                              const QSqlRecord record = query.record();
                              for (int i = 0, recCount = record.count() ; i < recCount ; ++i) {
                                  if (i > 0) {
                                      outStream << ',';
                                  }
                                  outStream << escapedCSV(record.value(i).toString());
                              }
                              outStream << '\n';
                          }
                      }
                      
                      1 Reply Last reply Reply Quote 2
                      • A
                        AliM93 last edited by

                        thank you all!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post