Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Deploying Qt app with sqlite database to android phone
QtWS25 Last Chance

Deploying Qt app with sqlite database to android phone

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
4 Posts 2 Posters 1.9k 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.
  • K Offline
    K Offline
    kasmax99
    wrote on last edited by aha_1980
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      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
      0
      • K Offline
        K Offline
        kasmax99
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          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
          1

          • Login

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