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. [Solved] QT5.1 / Android - How to Bundle and use SQLite database in application.
QtWS25 Last Chance

[Solved] QT5.1 / Android - How to Bundle and use SQLite database in application.

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 3 Posters 18.4k 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.
  • S Offline
    S Offline
    sanjaysampat
    wrote on 11 Jul 2013, 14:22 last edited by
    #1

    I have created a sample test application. It uses the SqLite database. query it and returns me the data of records to display in edit box.

    To bundle the database, I used the folder ''android/assets' in 'Other files' in the project. I kept the SqLite file there.

    I have used Application Home Location folder to store the database after the installation.

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    // any other init .....
    //find if database exists at user's home directory location
    tmpString = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    QFileInfo databaseFileInfo(QString("%1/%2").arg(tmpString).arg("databaseFileName"));
    databasePath = databaseFileInfo.absoluteFilePath();
    ui->msgTextEdit->append("databasePath: "+databasePath); // to display full name with path of database
    if ( !databaseFileInfo.exists() )
    {
    ui->msgTextEdit->append("Database does not exist");
    bool copySuccess = QFile::copy( QString("assets:/databaseFileName"), databasePath );
    if ( !copySuccess )
    {
    QMessageBox::critical(this, "Error:", QString("Could not copy database from 'assets' to %1").arg(databasePath));
    databasePath.clear();
    }
    }
    }

    void MainWindow::on_loadDatabasePushButton_clicked()
    {
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

        QMessageBox mb;
        QSqlDatabase myDB = QSqlDatabase::addDatabase("QSQLITE");
        if( !myDB.isValid() )
        {
            // This shows a message Box if Database Connection Fails.
            QString str;
            str = databasePath;
            str.prepend(" Cannot Add database ");
            mb.setText(str);
            mb.exec();
        }
        //myDB.setHostName("localhost");
        myDB.setDatabaseName(databasePath);
        //myDB.setUserName("username");
        //myDB.setPassword("password");
        if (!myDB.open())
        {
            // This shows a message if System is unable to Open created Database
            mb.setText("Cannot open database  ");
            mb.exec();
            qDebug(&#41; << myDB.lastError(&#41;<<"not created";
        }
    
        QString qryStr;
        QSqlQuery query(myDB);
        qryStr = QString("SELECT Id,  Name, Description FROM Table1");
        query.prepare(qryStr);
        if( !query.exec&#40;qryStr&#41; &#41;
            qDebug(&#41; << query.lastError();
        else
        {
            ui->msgTextEdit->append(QString("\nThe records in Table1:"));
            while(query.next())
            {
                ui->msgTextEdit->append(QString("\nId:%1  Name:%2  Desc:%3")
                                        .arg(query.value("Id").toString())
                                        .arg(query.value("Name").toString())
                                        .arg(query.value("Description").toString())
                                        );
            }
         }
    
        QApplication::restoreOverrideCursor();
    

    }
    @

    tmpString and databasePath are class level QString variables.

    Best Regards

    1 Reply Last reply
    1
    • A Offline
      A Offline
      AlterX
      wrote on 19 Jul 2013, 08:10 last edited by
      #2

      Is this a working solution?

      Qt Ambassador
      Real-time cooperative teams: http://www.softairrealfight.net
      Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

      https://codereview.qt-project.org/...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sanjaysampat
        wrote on 22 Jul 2013, 13:06 last edited by
        #3

        Yes, this is working sample.
        the mainwindow.cpp has the following include...
        @
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QDebug>
        #include <QFile>
        #include <QFileInfo>
        #include <QMessageBox>
        #include <QStandardPaths>
        #include <QtSql>
        @

        It is required to create a sqlite Database called "databaseFileName"

        As mentioned earlier
        when you are in Android build, you will have "Other files" folder created in your project, where <myProject>.pro file exists.

        add the database file in the folder "<myProject>\android\assets"

        Build ... Deploy and run ...

        (I have NOT tested it on emulator.)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AlterX
          wrote on 22 Jul 2013, 13:13 last edited by
          #4

          Ok thanks a lot...I was just searching for something!
          I'm just speaking on bugtrack about assets for android...I think is not so portable thinking about ios too...they need to abstract DEPLOYMENTFOLDERS to automatically copy files in right path (android/assets and wherever) for different systems.

          Qt Ambassador
          Real-time cooperative teams: http://www.softairrealfight.net
          Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

          https://codereview.qt-project.org/...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adrz1
            wrote on 15 Nov 2013, 15:39 last edited by
            #5

            Hello,

            What are the implications of copying the database from assets to other location?

            thanks

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AlterX
              wrote on 18 Nov 2013, 14:53 last edited by
              #6

              There is no implications...but Android, like iOS, has the concept of blackbox, that is, you cannot write something outside app's directory.

              Qt Ambassador
              Real-time cooperative teams: http://www.softairrealfight.net
              Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

              https://codereview.qt-project.org/...

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adrz1
                wrote on 20 Nov 2013, 17:35 last edited by
                #7

                Thanks, i don't know if i did something wrong, but for me this is needed to fully operate the database:

                @QFile::setPermissions(databasePath, QFile::ReadOwner | QFile::WriteOwner);@

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AlterX
                  wrote on 20 Nov 2013, 18:30 last edited by
                  #8

                  That is interesting and possible since a friend of mine has had a permission denied.

                  Qt Ambassador
                  Real-time cooperative teams: http://www.softairrealfight.net
                  Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                  https://codereview.qt-project.org/...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sanjaysampat
                    wrote on 29 Nov 2013, 11:42 last edited by
                    #9

                    Please take care for QT 5.2 RC1 related possible changes
                    ( following copied from
                    "http://qt-project.org/wiki/Qt_for_Android_known_issues" )

                    • The way Qt for Android projects are structured has changed in Qt 5.2.0 compared to Qt 5.1.x. An android/ directory is no longer added to your project, and the contents of this directory is no longer automatically the source of the Android APK package. Instead, the source of the project is maintained elsewhere, and you can add or overwrite parts of it if you need to by using the ANDROID_PACKAGE_SOURCE_DIR qmake variable. It’s possible to port old projects to the new system by setting ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android in the .pro file, but you should then take care to remove all unneeded files from the android/ directory (i.e. only keep the files which contain modifications particular to your project.) For instance, if any of the Qt libraries or .jar files are left over in this directory, this will cause conflicts when building the project.
                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AlterX
                      wrote on 29 Nov 2013, 11:56 last edited by
                      #10

                      But using DEPLOYMENTFOLDERS will it automatically create android/assets in target device?

                      Qt Ambassador
                      Real-time cooperative teams: http://www.softairrealfight.net
                      Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                      https://codereview.qt-project.org/...

                      1 Reply Last reply
                      0

                      • Login

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