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. no query unable to fetch row

no query unable to fetch row

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 3.6k 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.
  • C ChrisW67
    30 Dec 2022, 05:50

    @hubeytqew QSqlDatabase using the Sqlite driver will open or create, read, write, and manage the file you set with setDatabaseName().

    Your code should not assume the QSqlDatabase::open() call returns true and just carry on regardless.

    if (dbcreate.open()) {
        QSqlQuery qry;
        qry.prepare("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
        if(!qry.exec())    {
            QMessageBox::warning(this, tr("error::"), qry.lastError().text());
        }
    }
    else  {
    // report the failure to open
    }
    

    Be careful if you are using relative paths to identify the database file. The process current working directory may not be where you think it is.

    As for the title of this thread, "no query unable to fetch row", nothing in the code presented attempts to fetch anything. The premise of the code presented is that the database does not exist.

    H Offline
    H Offline
    hubeytqew
    wrote on 30 Dec 2022, 06:44 last edited by
    #5

    @ChrisW67
    I know what is the purpose of the code and weird error about it actually the point of I can not understand.

    The code you write is getting same error. There is no problem with the location of the db file. Also, I use sqlite database.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 30 Dec 2022, 06:52 last edited by
      #6

      I assume the the call to prepare() is failing.

      You generally use prepare() on data manipulation queries (SELECT, UPDATE, DELETE), potentially with parameters, and not data definition queries (CREATE TABLE etc.)
      Try removing QSqlQuery and executing the SQL directly using QSqlDatabase::exec().

      H 1 Reply Last reply 30 Dec 2022, 07:00
      0
      • C ChrisW67
        30 Dec 2022, 06:52

        I assume the the call to prepare() is failing.

        You generally use prepare() on data manipulation queries (SELECT, UPDATE, DELETE), potentially with parameters, and not data definition queries (CREATE TABLE etc.)
        Try removing QSqlQuery and executing the SQL directly using QSqlDatabase::exec().

        H Offline
        H Offline
        hubeytqew
        wrote on 30 Dec 2022, 07:00 last edited by
        #7

        @ChrisW67

            if(!QFileInfo::exists("location/database.db"))
            {
                QFile file("location/database.db");
                file.open(QIODevice::ReadWrite);
                QSqlDatabase dbcreate = QSqlDatabase::addDatabase("QSQLITE");
                dbcreate.setDatabaseName("location/database.db");
                if(dbcreate.open())
                {
                    dbcreate.exec("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                }
            }
        

        still there is no table in the db file..interesting

        C J 2 Replies Last reply 30 Dec 2022, 08:19
        0
        • H hubeytqew
          30 Dec 2022, 07:00

          @ChrisW67

              if(!QFileInfo::exists("location/database.db"))
              {
                  QFile file("location/database.db");
                  file.open(QIODevice::ReadWrite);
                  QSqlDatabase dbcreate = QSqlDatabase::addDatabase("QSQLITE");
                  dbcreate.setDatabaseName("location/database.db");
                  if(dbcreate.open())
                  {
                      dbcreate.exec("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                  }
              }
          

          still there is no table in the db file..interesting

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 30 Dec 2022, 08:19 last edited by
          #8

          @hubeytqew said in no query unable to fetch row:

              QFile file("location/database.db");
              file.open(QIODevice::ReadWrite);
          

          still there is no table in the db file..interesting

          Because you still have the same error in there - remove the QFile usage!

          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
          0
          • H hubeytqew
            30 Dec 2022, 07:00

            @ChrisW67

                if(!QFileInfo::exists("location/database.db"))
                {
                    QFile file("location/database.db");
                    file.open(QIODevice::ReadWrite);
                    QSqlDatabase dbcreate = QSqlDatabase::addDatabase("QSQLITE");
                    dbcreate.setDatabaseName("location/database.db");
                    if(dbcreate.open())
                    {
                        dbcreate.exec("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                    }
                }
            

            still there is no table in the db file..interesting

            J Offline
            J Offline
            JonB
            wrote on 30 Dec 2022, 09:44 last edited by JonB
            #9

            @hubeytqew said in no query unable to fetch row:

            still there is no table in the db file..interesting

            You do not try to show a message/error if the dbcreate.open() fails nor do you test and report an error if the dbcreate.exec() fails. This is very unhelpful both to you and us. Error checking, especially during development, is vital.

            Two people said you should not try to create the file via QFile.open(), yet you still do so. Why not follow peoples' advice?

            H 1 Reply Last reply 2 Jan 2023, 05:23
            0
            • J JonB
              30 Dec 2022, 09:44

              @hubeytqew said in no query unable to fetch row:

              still there is no table in the db file..interesting

              You do not try to show a message/error if the dbcreate.open() fails nor do you test and report an error if the dbcreate.exec() fails. This is very unhelpful both to you and us. Error checking, especially during development, is vital.

              Two people said you should not try to create the file via QFile.open(), yet you still do so. Why not follow peoples' advice?

              H Offline
              H Offline
              hubeytqew
              wrote on 2 Jan 2023, 05:23 last edited by hubeytqew 1 Feb 2023, 05:29
              #10

              @JonB @Christian-Ehrlicher
              I havent seen christian's first "dont use qfile" message or I missed it. Now, I remove QFile line.

                  if(!QFileInfo::exists("location/database.db"))
                      {
                          QSqlDatabase dbcreate = QSqlDatabase::addDatabase("QSQLITE");
                          dbcreate.setDatabaseName("location/database.db");
                          if(dbcreate.open())
                          {
                              QSqlQuery qry;
                              qry.prepare("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                              if(!qry.exec())
                              {
                                  QMessageBox::warning(this, tr("error::"), qry.lastError().text());
                              }
                          }
                          else
                          {
                              QMessageBox::warning(this, "Error", "Error here");
                          }
                      }
              

              if there is no database file, it is creating by the code, ok. But "No query unable to fetch row" error still here. Btw, there is no table inside of it.

              @JonB said in no query unable to fetch row:

              You do not try to show a message/error if the dbcreate.open() fails nor do you test and report an error if the dbcreate.exec() fails.

              I add show message if dbcreate.open() fail but actually it dont.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on 2 Jan 2023, 05:48 last edited by
                #11

                @ChrisW67 said in no query unable to fetch row:

                Be careful if you are using relative paths to identify the database file. The process current working directory may not be where you think it is.

                Just repeating my earlier warning.

                As an experiment, make the database path absolute (e.g. "/tmp/location.db") and check that file is created, and contains your table.

                H 1 Reply Last reply 2 Jan 2023, 06:09
                0
                • C ChrisW67
                  2 Jan 2023, 05:48

                  @ChrisW67 said in no query unable to fetch row:

                  Be careful if you are using relative paths to identify the database file. The process current working directory may not be where you think it is.

                  Just repeating my earlier warning.

                  As an experiment, make the database path absolute (e.g. "/tmp/location.db") and check that file is created, and contains your table.

                  H Offline
                  H Offline
                  hubeytqew
                  wrote on 2 Jan 2023, 06:09 last edited by hubeytqew 1 Feb 2023, 06:12
                  #12

                  @ChrisW67 the file location is a folder which exist on desktop.
                  "C:/Users/myusername/Desktop/project/database.db"
                  If database file not in the project folder, it is creating but there is no table inside of it.

                  C 1 Reply Last reply 2 Jan 2023, 06:25
                  0
                  • H hubeytqew
                    2 Jan 2023, 06:09

                    @ChrisW67 the file location is a folder which exist on desktop.
                    "C:/Users/myusername/Desktop/project/database.db"
                    If database file not in the project folder, it is creating but there is no table inside of it.

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on 2 Jan 2023, 06:25 last edited by
                    #13

                    @hubeytqew Did you do as I asked, or are you just stating what you think is happening?

                    BTW: Your original code, with the unnecessary QFile lines, would create an empty file regardless of what the QSqlDatabase driver was doing.

                    H 1 Reply Last reply 2 Jan 2023, 06:53
                    0
                    • C ChrisW67
                      2 Jan 2023, 06:25

                      @hubeytqew Did you do as I asked, or are you just stating what you think is happening?

                      BTW: Your original code, with the unnecessary QFile lines, would create an empty file regardless of what the QSqlDatabase driver was doing.

                      H Offline
                      H Offline
                      hubeytqew
                      wrote on 2 Jan 2023, 06:53 last edited by hubeytqew 1 Feb 2023, 07:05
                      #14

                      @ChrisW67
                      now, I do what you say. Result did not changed.
                      C:/Temp/database.db is the location

                      @ChrisW67 said in no query unable to fetch row:

                      BTW: Your original code, with the unnecessary QFile lines, would create an empty file regardless of what the QSqlDatabase driver was doing.

                      Ok, I get it. I am creating file with setDatabaseName(). QFile is not exist anymore.

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hubeytqew
                        wrote on 2 Jan 2023, 07:48 last edited by
                        #15

                        .......this is so annoying.... I missed paranthesis :D:D:D:D:D

                        @hubeytqew said in no query unable to fetch row:

                        @JonB @Christian-Ehrlicher
                        I havent seen christian's first "dont use qfile" message or I missed it. Now, I remove QFile line.

                        qry.prepare("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                        

                        There should be one more paranthesis ("...., key varchar(64))");
                        .................................................................................................................................^

                        So,

                        @ChrisW67 said in no query unable to fetch row:

                        You generally use prepare() on data manipulation queries (SELECT, UPDATE, DELETE), potentially with parameters, and not data definition queries (CREATE TABLE etc.)
                        Try removing QSqlQuery and executing the SQL directly using QSqlDatabase::exec().

                        prepare() and QSqlDatabase::exec() and QSqlQuery::exec() now working correctly.

                        @ChrisW67 said in no query unable to fetch row:

                        @hubeytqew Did you do as I asked, or are you just stating what you think is happening?

                        There is no problem with the location at all.
                        The problem was elsewhere....

                        J 1 Reply Last reply 2 Jan 2023, 09:21
                        0
                        • H hubeytqew
                          2 Jan 2023, 07:48

                          .......this is so annoying.... I missed paranthesis :D:D:D:D:D

                          @hubeytqew said in no query unable to fetch row:

                          @JonB @Christian-Ehrlicher
                          I havent seen christian's first "dont use qfile" message or I missed it. Now, I remove QFile line.

                          qry.prepare("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(64), password varchar(64), source varchar(64), key varchar(64)");
                          

                          There should be one more paranthesis ("...., key varchar(64))");
                          .................................................................................................................................^

                          So,

                          @ChrisW67 said in no query unable to fetch row:

                          You generally use prepare() on data manipulation queries (SELECT, UPDATE, DELETE), potentially with parameters, and not data definition queries (CREATE TABLE etc.)
                          Try removing QSqlQuery and executing the SQL directly using QSqlDatabase::exec().

                          prepare() and QSqlDatabase::exec() and QSqlQuery::exec() now working correctly.

                          @ChrisW67 said in no query unable to fetch row:

                          @hubeytqew Did you do as I asked, or are you just stating what you think is happening?

                          There is no problem with the location at all.
                          The problem was elsewhere....

                          J Offline
                          J Offline
                          JonB
                          wrote on 2 Jan 2023, 09:21 last edited by
                          #16

                          @hubeytqew said in no query unable to fetch row:

                          .......this is so annoying.... I missed paranthesis :D:D:D:D:D

                          And did you check the return result from qry.prepare()? As advised, suggest you always check every return result available, especially when developing/something goes wrong....

                          M 1 Reply Last reply 22 Apr 2023, 06:59
                          1
                          • J JonB
                            2 Jan 2023, 09:21

                            @hubeytqew said in no query unable to fetch row:

                            .......this is so annoying.... I missed paranthesis :D:D:D:D:D

                            And did you check the return result from qry.prepare()? As advised, suggest you always check every return result available, especially when developing/something goes wrong....

                            M Offline
                            M Offline
                            Malachi
                            wrote on 22 Apr 2023, 06:59 last edited by
                            #17

                            I was having a similar issue, but in my case I didn't realize that the QSqlQuery constructor executes the SQL immediately if you pass in the string.

                            That created the "unable to fetch row" condition when I subsequently called .exec since I was erroneously instructing it to run the same query again.

                            1 Reply Last reply
                            1

                            • Login

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