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. Deploy Sqlite database with apk
Forum Updated to NodeBB v4.3 + New Features

Deploy Sqlite database with apk

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 4 Posters 5.1k Views 4 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.
  • B Offline
    B Offline
    behruz montazeri
    wrote on 28 Aug 2018, 07:43 last edited by behruz montazeri
    #1

    Where can I locate installed android apk on my Android mobile. when i deploy apk the sqlite database file not embedded with apk file.
    I open database file locally :

    db.setDatabaseName("/home/behruz/Desktop/Temp/QSqlQueryModeTableView/Poem.db");
    

    I read some article on web and i add this codes to .pro file but the problem stays.

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
    
     deployment.files += Poem.db
     deployment.path = /android/assets
     INSTALLS += deployment
    

    I uploaded the project : Here
    when i deploy on android device database is not included.Please help me.
    I asked Here before.

    R 1 Reply Last reply 28 Aug 2018, 11:38
    0
    • B behruz montazeri
      28 Aug 2018, 07:43

      Where can I locate installed android apk on my Android mobile. when i deploy apk the sqlite database file not embedded with apk file.
      I open database file locally :

      db.setDatabaseName("/home/behruz/Desktop/Temp/QSqlQueryModeTableView/Poem.db");
      

      I read some article on web and i add this codes to .pro file but the problem stays.

      ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
      
       deployment.files += Poem.db
       deployment.path = /android/assets
       INSTALLS += deployment
      

      I uploaded the project : Here
      when i deploy on android device database is not included.Please help me.
      I asked Here before.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 28 Aug 2018, 11:38 last edited by raven-worx
      #2

      @behruz-montazeri
      AFAIK files placed $$PWD/android/assets should be included automatically in your APK.
      But anyway, i believe you wont be able to load the db file from the assets folder anyway.
      Correct me if i am wrong, but you need write access on the db file (even when you only reading from the database).

      You would need to add first-run-logic to copy the db file from the assets folder to the apps writeable (private data) location.
      Which leads you to the possibility to include the database file via the Qt resource system instead of Android's asset system. Should be easier in the end.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • B Offline
        B Offline
        behruz montazeri
        wrote on 28 Aug 2018, 12:08 last edited by behruz montazeri
        #3

        I created a folder under android folder assests
        this is my .pro file :

        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android/assets
        
        deployment.files += Poem.db
        deployment.path = /android/assets
        INSTALLS += deployment
        

        and database connection file :

            QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName("Poem.db");
            QFile dfile("assets:/Poem.db");
            if (dfile.exists())
            {
                dfile.copy("./Poem.db");
                QFile::setPermissions("./Poem.db",QFile::WriteOwner |     QFile::ReadOwner);
             }
        

        The database is missing when i run and install via qtcreator
        0_1535458393265_qtcreator.png

        R 1 Reply Last reply 28 Aug 2018, 12:13
        0
        • B behruz montazeri
          28 Aug 2018, 12:08

          I created a folder under android folder assests
          this is my .pro file :

          ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android/assets
          
          deployment.files += Poem.db
          deployment.path = /android/assets
          INSTALLS += deployment
          

          and database connection file :

              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.setDatabaseName("Poem.db");
              QFile dfile("assets:/Poem.db");
              if (dfile.exists())
              {
                  dfile.copy("./Poem.db");
                  QFile::setPermissions("./Poem.db",QFile::WriteOwner |     QFile::ReadOwner);
               }
          

          The database is missing when i run and install via qtcreator
          0_1535458393265_qtcreator.png

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 28 Aug 2018, 12:13 last edited by raven-worx
          #4

          @behruz-montazeri said in Deploy Sqlite database with apk:

          ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android/assets

          wrong. should be $$PWD/android only

          deployment.path = /android/assets

          Should be $PWD/android/assets IMHO

          dfile.copy("./Poem.db");

          won't work. Since this is not a valid writeable location in the Android system.
          Also you should do the copying before you initialize the database.

          You may want to read this.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          B 1 Reply Last reply 29 Aug 2018, 04:57
          2
          • B Offline
            B Offline
            behruz montazeri
            wrote on 28 Aug 2018, 12:25 last edited by
            #5

            Still not works. Please if you got free time look at my project : Here

            R 1 Reply Last reply 28 Aug 2018, 12:31
            0
            • B behruz montazeri
              28 Aug 2018, 12:25

              Still not works. Please if you got free time look at my project : Here

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 28 Aug 2018, 12:31 last edited by raven-worx
              #6

              @behruz-montazeri
              i don't see why you need to use the Android asset approach instead the qrc approach i mentioned.
              And why you need to copy the files dynamically to the assets folder?!

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • B Offline
                B Offline
                behruz montazeri
                wrote on 28 Aug 2018, 12:36 last edited by
                #7

                I really appreciate if explain qrc approach. I didn't get it.

                R 1 Reply Last reply 28 Aug 2018, 12:39
                0
                • B behruz montazeri
                  28 Aug 2018, 12:36

                  I really appreciate if explain qrc approach. I didn't get it.

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 28 Aug 2018, 12:39 last edited by
                  #8

                  @behruz-montazeri
                  the Qt resource system
                  The file(s) gets compiled into the binary and can be accessed via e.g. QFile f(":/my.db")

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  B 1 Reply Last reply 28 Aug 2018, 12:56
                  0
                  • R raven-worx
                    28 Aug 2018, 12:39

                    @behruz-montazeri
                    the Qt resource system
                    The file(s) gets compiled into the binary and can be accessed via e.g. QFile f(":/my.db")

                    B Offline
                    B Offline
                    behruz montazeri
                    wrote on 28 Aug 2018, 12:56 last edited by
                    #9

                    @raven-worx
                    I really got confused. Is it possible to write me the codes or correct me via github.
                    This issue still is complicated for me and i don't know what should i do.
                    When i install the app on my phone via qtcreator i could't find the installed files on my phone to add database file manually. Where is the installed folder

                    sierdzioS 1 Reply Last reply 28 Aug 2018, 13:48
                    0
                    • B behruz montazeri
                      28 Aug 2018, 12:56

                      @raven-worx
                      I really got confused. Is it possible to write me the codes or correct me via github.
                      This issue still is complicated for me and i don't know what should i do.
                      When i install the app on my phone via qtcreator i could't find the installed files on my phone to add database file manually. Where is the installed folder

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on 28 Aug 2018, 13:48 last edited by
                      #10

                      @behruz-montazeri said in Deploy Sqlite database with apk:

                      . Where is the installed folder

                      Android keeps it hidden. Only your application, when it runs, is able to get there (unless your phone is rooted).

                      And the location is listed in https://doc.qt.io/qt-5/qstandardpaths.html

                      Regarding code, I don't have any that would make a minimal example for you, but: just right click on your project in Qt Creator, select "Add new...", then Qt Resource file. Once you have it, add your .db to the resource. Remember, resources are read only. Then you can copy it to one of the standard paths (preferably QStandardPaths::AppDataLocation) using QFile and then you can open it with write permissions (using QSql for example).

                      (Z(:^

                      1 Reply Last reply
                      2
                      • R raven-worx
                        28 Aug 2018, 12:13

                        @behruz-montazeri said in Deploy Sqlite database with apk:

                        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android/assets

                        wrong. should be $$PWD/android only

                        deployment.path = /android/assets

                        Should be $PWD/android/assets IMHO

                        dfile.copy("./Poem.db");

                        won't work. Since this is not a valid writeable location in the Android system.
                        Also you should do the copying before you initialize the database.

                        You may want to read this.

                        B Offline
                        B Offline
                        behruz montazeri
                        wrote on 29 Aug 2018, 04:57 last edited by behruz montazeri 9 Mar 2018, 11:44
                        #11

                        @raven-worx
                        Thanks. With the help of this page i solve the problem.

                        .pro file :

                        ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
                        
                        android
                        {
                        my_files.path = /assets
                        my_files.files = $$PWD/android/*
                        INSTALLS += my_files
                        }
                        

                        connection.cpp

                            QFile DbFile;
                            QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
                            DbFile.setFileName("assets:/Poem.db");
                            DbFile.copy(DatabaseDataStoragePath + "/Poem.db");
                            QFile::setPermissions(DatabaseDataStoragePath + "/Poem.db", QFile::WriteOwner | QFile::ReadOwner);
                        
                                QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                                db.setDatabaseName("Poem.db");
                                if (!db.open()) {
                                    QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                                        QObject::tr("Unable to establish a database connection.\n"
                                                    "This example needs SQLite support. Please read "
                                                    "the Qt SQL driver documentation for information how "
                                                    "to build it.\n\n"
                                                    "Click Cancel to exit."), QMessageBox::Cancel);
                                    return false;
                                }
                        
                        Pablo J. RoginaP 1 Reply Last reply 29 Aug 2018, 15:31
                        1
                        • B behruz montazeri
                          29 Aug 2018, 04:57

                          @raven-worx
                          Thanks. With the help of this page i solve the problem.

                          .pro file :

                          ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
                          
                          android
                          {
                          my_files.path = /assets
                          my_files.files = $$PWD/android/*
                          INSTALLS += my_files
                          }
                          

                          connection.cpp

                              QFile DbFile;
                              QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
                              DbFile.setFileName("assets:/Poem.db");
                              DbFile.copy(DatabaseDataStoragePath + "/Poem.db");
                              QFile::setPermissions(DatabaseDataStoragePath + "/Poem.db", QFile::WriteOwner | QFile::ReadOwner);
                          
                                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                                  db.setDatabaseName("Poem.db");
                                  if (!db.open()) {
                                      QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                                          QObject::tr("Unable to establish a database connection.\n"
                                                      "This example needs SQLite support. Please read "
                                                      "the Qt SQL driver documentation for information how "
                                                      "to build it.\n\n"
                                                      "Click Cancel to exit."), QMessageBox::Cancel);
                                      return false;
                                  }
                          
                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on 29 Aug 2018, 15:31 last edited by
                          #12

                          @behruz-montazeri please don't forget to mark your posted as solved! thanks.

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          1

                          1/12

                          28 Aug 2018, 07:43

                          • Login

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