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

Deploy Sqlite database with apk



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


  • Moderators

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



  • 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


  • Moderators

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



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


  • Moderators

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



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


  • Moderators

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



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


  • Moderators

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



  • @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;
            }
    


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


Log in to reply