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

no query unable to fetch row

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 3.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.
  • H Offline
    H Offline
    hubeytqew
    wrote on last edited by hubeytqew
    #1

    I want to create a db file then create a table if the db file does not exists...so for the first run.
    Where is the problem? I get the error in the if(!qry.exec()) line. I think error comes due to the create db file part. Which is file.open(QIODevice::ReadWrite)

    So I think, I can not create and open file properly.

    if(!QFileInfo::exists("some_location/database.db"))
        {
            QFile file("some_location/database.db");
            file.open(QIODevice::ReadWrite);
            QSqlDatabase dbcreate = QSqlDatabase::addDatabase("QSQLITE");
            dbcreate.setDatabaseName("some_location/database.db");
            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());
            }
        }
    
    1 Reply Last reply
    0
    • H Offline
      H Offline
      hubeytqew
      wrote on 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....

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

        Check the return value of dbcreate.open() and don't open the database with a QFile (why do you need to open the database file with QFile at all?)

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

        H 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Check the return value of dbcreate.open() and don't open the database with a QFile (why do you need to open the database file with QFile at all?)

          H Offline
          H Offline
          hubeytqew
          wrote on last edited by
          #3

          @Christian-Ehrlicher
          if I dont open a file, how can I read&write this file, idk? which method should I open a file?
          dbcreate.open() return value is true.

          C 1 Reply Last reply
          0
          • H hubeytqew

            @Christian-Ehrlicher
            if I dont open a file, how can I read&write this file, idk? which method should I open a file?
            dbcreate.open() return value is true.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @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 1 Reply Last reply
            0
            • C ChrisW67

              @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 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 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
                0
                • C ChrisW67

                  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 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

                  Christian EhrlicherC JonBJ 2 Replies Last reply
                  0
                  • H hubeytqew

                    @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

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                      @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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      0
                      • JonBJ JonB

                        @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 last edited by hubeytqew
                        #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 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
                          0
                          • C ChrisW67

                            @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 last edited by hubeytqew
                            #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
                            0
                            • H hubeytqew

                              @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 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
                              0
                              • C ChrisW67

                                @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 last edited by hubeytqew
                                #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 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....

                                  JonBJ 1 Reply Last reply
                                  0
                                  • H hubeytqew

                                    .......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....

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 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
                                    1
                                    • JonBJ JonB

                                      @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 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