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

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.2k Views 2 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.
  • Hasibul Hasan ChowdhuryH Offline
    Hasibul Hasan ChowdhuryH Offline
    Hasibul Hasan Chowdhury
    wrote on last edited by
    #1

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

    the_T 1 Reply Last reply
    0
    • Hasibul Hasan ChowdhuryH Hasibul Hasan Chowdhury

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

      the_T Offline
      the_T Offline
      the_
      wrote on 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 --

      Hasibul Hasan ChowdhuryH 1 Reply Last reply
      0
      • the_T the_

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

        Hasibul Hasan ChowdhuryH Offline
        Hasibul Hasan ChowdhuryH Offline
        Hasibul Hasan Chowdhury
        wrote on 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);
        

        }

        Hasibul Hasan ChowdhuryH J.HilkJ 2 Replies Last reply
        0
        • Hasibul Hasan ChowdhuryH Hasibul Hasan Chowdhury

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

          }

          Hasibul Hasan ChowdhuryH Offline
          Hasibul Hasan ChowdhuryH Offline
          Hasibul Hasan Chowdhury
          wrote on 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 ();
          }
          

          }

          the_T 1 Reply Last reply
          0
          • Hasibul Hasan ChowdhuryH Hasibul Hasan Chowdhury

            @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 ();
            }
            

            }

            the_T Offline
            the_T Offline
            the_
            wrote on 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
            • Hasibul Hasan ChowdhuryH Hasibul Hasan Chowdhury

              @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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on 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.

              the_T 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                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.

                the_T Offline
                the_T Offline
                the_
                wrote on 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.HilkJ 1 Reply Last reply
                0
                • the_T the_

                  @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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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 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

                    • Login

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