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.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on 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

    raven-worxR 1 Reply Last reply
    0
    • behruz montazeriB behruz montazeri

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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

      behruz montazeriB 1 Reply Last reply
      2
      • behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by
        #5

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

        raven-worxR 1 Reply Last reply
        0
        • behruz montazeriB behruz montazeri

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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
          • behruz montazeriB Offline
            behruz montazeriB Offline
            behruz montazeri
            wrote on last edited by
            #7

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

            raven-worxR 1 Reply Last reply
            0
            • behruz montazeriB behruz montazeri

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

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on 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

              behruz montazeriB 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @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")

                behruz montazeriB Offline
                behruz montazeriB Offline
                behruz montazeri
                wrote on 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
                0
                • behruz montazeriB behruz montazeri

                  @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 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
                  • raven-worxR raven-worx

                    @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.

                    behruz montazeriB Offline
                    behruz montazeriB Offline
                    behruz montazeri
                    wrote on last edited by behruz montazeri
                    #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
                    1
                    • behruz montazeriB behruz montazeri

                      @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 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

                      • Login

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