Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Deploying Qt app with sqlite database to android phone



  • Hi Guys
    I am new to Qt and am not an expert so I really need your help.
    I developed small app in Qt that connects to SQLite database and check login.
    It works fine on the desktop. but after I deploy it to android phone the login button does nothing
    seems like maybe it doesn't have permissions to read or write.

    below is my code:

    SqlTest.pro:bolded text
    QT += core gui
    QT += sql

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = SqlTest
    TEMPLATE = app

    CONFIG += c++11

    SOURCES +=
    main.cpp
    mainwindow.cpp

    database.sources = "KFinance.db"
    database.path = .

    DEPLOYMENT += database

    HEADERS +=
    mainwindow.h

    FORMS +=
    mainwindow.ui

    CONFIG += mobility
    MOBILITY =

    Default rules for deployment.

    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    android { QMAKE_LFLAGS += -nostdlib++ }

    mainwindow.h:bolded text
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtSql>
    #include <QDebug>
    #include <QFileInfo>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    QSqlDatabase mydb;

    void connClose()
    {
       mydb.close();
       mydb.removeDatabase(QSqlDatabase::defaultConnection);
    }
    
    
    bool connOpen()
    {
        QString dbfilepath = QDir::currentPath()+"/KFinance.db";
         if(!QFile::exists(dbfilepath))
         {
         QFile::copy(":/db/KFinance.db", QDir::currentPath()+"/KFinance.db");
         QFile::setPermissions(QDir::currentPath()+"/KFinance.db",
         QFile::ReadOwner|QFile::WriteOwner);
         }
    
        mydb=QSqlDatabase::addDatabase("QSQLITE");
        //mydb.setDatabaseName("C:/Users/Kas/Documents/KFinance/KFinance.db");
        mydb.setDatabaseName("KFinance.db");
    
        if(!mydb.open())
        {
            qDebug()<<("Not Connected");
            return false;
        }
    else
        {
            qDebug()<<("Connected");
            return true;
        }
    
    }
    

    public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    mainwindow.cppbolded text
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    if(!connOpen())
    ui->label->setText("Failed");
    else
    ui->label->setText("Connected");
    QString db_path = QDir::currentPath();
    ui->label_3->setText(db_path);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    QString username,password;
    username=ui->lineEdit_username->text();
    password=ui->lineEdit_password->text();

        if(!connOpen())
        {
            qDebug()<<"Failed";
            return;
        }
    
        connOpen();
        QSqlQuery qry;
        qry.prepare("select * from employeeinfo where username='"+username+"' and password='"+password+"'");
    
        if (qry.exec( ))
        {
          int count=0;
          while(qry.next())
            {
                count++;
            }
    
              if(count==1){
                     ui->label->setText("username and password correct");
                     connClose();
    
    
              }
              if(count>1)
                            ui->label->setText("username and password Duplicate");
              if(count<1)
                            ui->label->setText("username and password not correct");
        }
    

    }

    So can please some experts help me identify why it doesn't work on phone. it opens the app ok but when I enter user and pass and hot click button it does nothing.

    this is what I see in qt output window when I click on login button on phone while connected to pc via usb cable:
    D ViewRootImpl@b006591[QtActivity]: ViewPostIme pointer 0
    D ViewRootImpl@b006591[QtActivity]: ViewPostIme pointer 1
    W libSqlTest.so: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    W libSqlTest.so: QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    D libSqlTest.so: Connected
    W libSqlTest.so: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    W libSqlTest.so: QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    D libSqlTest.so: Connected

    Also is it possible if I can copy my SQLite db manually in sd card and then my qt code can link to it?

    I really appreciate your help
    Many thanks


  • Lifetime Qt Champion

    Hi,

    As long as the file is accessible then it can work.

    Note that it also means that the databases can also be read by other applications.



  • So how to make it accessible?
    Its deployed to phone by Qt.
    Looking at my code posted can someone please guide me?


  • Lifetime Qt Champion

    The problem is simple: you are trying to create the database in the same folder as your application. That won't work on mobile. It also won't work on desktop if you install the application in the system because usually they will be rightfully put in read-only places. You should use QStandardPaths to get a suitable folder for your application to write into.


Log in to reply