QT SQLite “No query Unable to fetch row” when try to build for android
-
My code work fine for desktop but give this error when run on android emulator. Where is the problem ?
-
@Hasibul-Hasan-Chowdhury
the USB crystall ball is broken, sorrycan 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?)
-
@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-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 (); }
}
-
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
-
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.
-
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?
-
@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. -
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.