Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QT SQLite “No query Unable to fetch row” when try to build for android

QT SQLite “No query Unable to fetch row” when try to build for android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 4 Posters 3.0k 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
    Hasibul Hasan Chowdhury
    wrote on 15 May 2017, 07:08 last edited by
    #1

    My code work fine for desktop but give this error when run on android emulator. Where is the problem ?

    T 1 Reply Last reply 15 May 2017, 07:15
    0
    • H Hasibul Hasan Chowdhury
      15 May 2017, 07:08

      My code work fine for desktop but give this error when run on android emulator. Where is the problem ?

      T Offline
      T Offline
      the_
      wrote on 15 May 2017, 07:15 last edited by
      #2

      @Hasibul-Hasan-Chowdhury
      the USB crystall ball is broken, sorry

      can you please give a little bit more information about how you try to open the database and execute your query (maybe the relevant piece of code?)

      -- No support in PM --

      H 1 Reply Last reply 15 May 2017, 07:38
      0
      • T the_
        15 May 2017, 07:15

        @Hasibul-Hasan-Chowdhury
        the USB crystall ball is broken, sorry

        can you please give a little bit more information about how you try to open the database and execute your query (maybe the relevant piece of code?)

        H Offline
        H Offline
        Hasibul Hasan Chowdhury
        wrote on 15 May 2017, 07:38 last edited by
        #3

        @the_ Thank you for your response. Here is my code
        #include "databasemanager.h"

        DatabaseManager::DatabaseManager()
        {
        openDB();
        }

        DatabaseManager::~DatabaseManager()
        {
        //close database after finish
        db.close ();
        }

        QSqlDatabase DatabaseManager::getDB()
        {
        return db;
        }

        QSqlQuery *DatabaseManager::getQuery()
        {
        return query;
        }

        void DatabaseManager::openDB()
        {
        //load database driver on create
        db = QSqlDatabase::addDatabase ("QSQLITE");

        //loading existing databse
        //db.setDatabaseName ("HCT_CMS.db");
        db.setDatabaseName ("./HCT_CMS.db");
        
        //opening database
        db.open ();
        
        //try connect to database
        if(db.isOpen ()){
            qDebug() << "Database connected.";
        } else {
            qDebug() << db.lastError ();
        }
        
        //query connected to specific database
        query = new QSqlQuery(db);
        

        }

        H J 2 Replies Last reply 15 May 2017, 07:46
        0
        • H Hasibul Hasan Chowdhury
          15 May 2017, 07:38

          @the_ Thank you for your response. Here is my code
          #include "databasemanager.h"

          DatabaseManager::DatabaseManager()
          {
          openDB();
          }

          DatabaseManager::~DatabaseManager()
          {
          //close database after finish
          db.close ();
          }

          QSqlDatabase DatabaseManager::getDB()
          {
          return db;
          }

          QSqlQuery *DatabaseManager::getQuery()
          {
          return query;
          }

          void DatabaseManager::openDB()
          {
          //load database driver on create
          db = QSqlDatabase::addDatabase ("QSQLITE");

          //loading existing databse
          //db.setDatabaseName ("HCT_CMS.db");
          db.setDatabaseName ("./HCT_CMS.db");
          
          //opening database
          db.open ();
          
          //try connect to database
          if(db.isOpen ()){
              qDebug() << "Database connected.";
          } else {
              qDebug() << db.lastError ();
          }
          
          //query connected to specific database
          query = new QSqlQuery(db);
          

          }

          H Offline
          H Offline
          Hasibul Hasan Chowdhury
          wrote on 15 May 2017, 07:46 last edited by
          #4

          @Hasibul-Hasan-Chowdhury
          void Dialog::login_to_System()
          {
          //get user inputs
          QString user_name = ui->txt_username->text ();
          QString pass = ui->txt_password->text ();

          //query statement for match username and password
          const QString checkstmnt = "SELECT Username, Password FROM administrator WHERE "
                                     "username = \'" + user_name + "\' AND password = \'" + pass + "\'";
          
          //prepare statement
          dbm->getQuery ()->prepare (checkstmnt);
          
          //if execute query successful or match with database
          if(dbm->getQuery ()->exec ()){
          
              //check if value is exists
              if(dbm->getQuery ()->next ()){
          
                  //show the main window after successfully logged in to system
                  MainWindow *mwindow = new MainWindow();
                  mwindow->show ();
          
                  //close the login window
                  this->close ();
          
                  //clear value
                  user_name.clear ();
                  pass.clear ();
          
              } else {
                  QMessageBox::critical (this, "Error !!!", "Wrong username or password. Try again.");
              }
          } else {
              QMessageBox::critical (this, "Error !!!", dbm->getQuery ()->lastError ().text ());
              qDebug() << dbm->getQuery ()->lastError ().text ();
          }
          

          }

          T 1 Reply Last reply 15 May 2017, 08:01
          0
          • H Hasibul Hasan Chowdhury
            15 May 2017, 07:46

            @Hasibul-Hasan-Chowdhury
            void Dialog::login_to_System()
            {
            //get user inputs
            QString user_name = ui->txt_username->text ();
            QString pass = ui->txt_password->text ();

            //query statement for match username and password
            const QString checkstmnt = "SELECT Username, Password FROM administrator WHERE "
                                       "username = \'" + user_name + "\' AND password = \'" + pass + "\'";
            
            //prepare statement
            dbm->getQuery ()->prepare (checkstmnt);
            
            //if execute query successful or match with database
            if(dbm->getQuery ()->exec ()){
            
                //check if value is exists
                if(dbm->getQuery ()->next ()){
            
                    //show the main window after successfully logged in to system
                    MainWindow *mwindow = new MainWindow();
                    mwindow->show ();
            
                    //close the login window
                    this->close ();
            
                    //clear value
                    user_name.clear ();
                    pass.clear ();
            
                } else {
                    QMessageBox::critical (this, "Error !!!", "Wrong username or password. Try again.");
                }
            } else {
                QMessageBox::critical (this, "Error !!!", dbm->getQuery ()->lastError ().text ());
                qDebug() << dbm->getQuery ()->lastError ().text ();
            }
            

            }

            T Offline
            T Offline
            the_
            wrote on 15 May 2017, 08:01 last edited by
            #5

            @Hasibul-Hasan-Chowdhury

            before reading the rest of the code, your sql query is widely open for injections. please read how to use QSqlQuery::prepare() correctly.

            NEVER pass user input directly to a sql query

            -- No support in PM --

            1 Reply Last reply
            2
            • H Hasibul Hasan Chowdhury
              15 May 2017, 07:38

              @the_ Thank you for your response. Here is my code
              #include "databasemanager.h"

              DatabaseManager::DatabaseManager()
              {
              openDB();
              }

              DatabaseManager::~DatabaseManager()
              {
              //close database after finish
              db.close ();
              }

              QSqlDatabase DatabaseManager::getDB()
              {
              return db;
              }

              QSqlQuery *DatabaseManager::getQuery()
              {
              return query;
              }

              void DatabaseManager::openDB()
              {
              //load database driver on create
              db = QSqlDatabase::addDatabase ("QSQLITE");

              //loading existing databse
              //db.setDatabaseName ("HCT_CMS.db");
              db.setDatabaseName ("./HCT_CMS.db");
              
              //opening database
              db.open ();
              
              //try connect to database
              if(db.isOpen ()){
                  qDebug() << "Database connected.";
              } else {
                  qDebug() << db.lastError ();
              }
              
              //query connected to specific database
              query = new QSqlQuery(db);
              

              }

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 15 May 2017, 08:04 last edited by
              #6

              My usb-Crystalball tells me this:

              db.setDatabaseName ("./HCT_CMS.db");

              you don't have to permission to read/write on your Android device. Check your manifest and if you're using api 24+ check your App-Settings on your device after installlation.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              T 1 Reply Last reply 15 May 2017, 08:26
              2
              • J J.Hilk
                15 May 2017, 08:04

                My usb-Crystalball tells me this:

                db.setDatabaseName ("./HCT_CMS.db");

                you don't have to permission to read/write on your Android device. Check your manifest and if you're using api 24+ check your App-Settings on your device after installlation.

                T Offline
                T Offline
                the_
                wrote on 15 May 2017, 08:26 last edited by
                #7

                @Hasibul-Hasan-Chowdhury

                Did you check if you already got an error message when trying to open the database?

                @J-Hilk I have never written code for Android but when trying to open/write a sqlite db to a place without write perissions on windows it would tell somethin like "out of memory". Same for Android?

                -- No support in PM --

                J 1 Reply Last reply 15 May 2017, 08:38
                0
                • T the_
                  15 May 2017, 08:26

                  @Hasibul-Hasan-Chowdhury

                  Did you check if you already got an error message when trying to open the database?

                  @J-Hilk I have never written code for Android but when trying to open/write a sqlite db to a place without write perissions on windows it would tell somethin like "out of memory". Same for Android?

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 15 May 2017, 08:38 last edited by
                  #8

                  @the_ said in QT SQLite “No query Unable to fetch row” when try to build for android:

                  @J-Hilk I have never written code for Android but when trying to open/write a sqlite db to a place without write perissions on windows it would tell somethin like "out of memory". Same for Android?

                  It's been a while since I wrote SQL for android.
                  But if memory serves me right, than there is no specific read/write access error. I believe I had no "our of memory" error but indeed the "unable to fetch row" one.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 15 May 2017, 20:30 last edited by
                    #9

                    Hi,

                    IIRC, the out of memory error is the error text returned by SQLite.

                    In any case, @J-Hilk is right, the path is wrong for Android.

                    QStandardPaths::writableLocation with QStandardPaths::AppDataLocation is the safest way.

                    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
                    3

                    1/9

                    15 May 2017, 07:08

                    • Login

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