[Solved] LocalStorage with custom path & android



  • Hi to all. I create Qt Quick Application and need to deploy it to Android Device. With application i have Sqlite database with some info which i'm using from LocalStorage in Qt Quick.

    In Desktop i do this stuff (main.cpp):
    @
    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setLocalStoragePath(QStringLiteral("qml/Project"));
    viewer.setMainQmlFile(QStringLiteral("qml/Project/main.qml"));
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @

    And for qtquick2applicationviewer.cpp
    @
    void QtQuick2ApplicationViewer::setLocalStoragePath(const QString &path)
    {
    engine()->setOfflineStoragePath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
    }
    @

    And in qml/Project i have Database subdir with files:
    c32516babc5b6c47eb8ce1bfc223253c.ini
    c32516babc5b6c47eb8ce1bfc223253c.sqlite

    Everything works fine on Desktop, but on Android i couldn't connect to this database. Any ideas?

    Thanks.


  • Moderators

    Hi,

    Is the path present on the filesystem ? Since as per docs,
    "Note that the path may not currently exist on the filesystem, so callers wanting to create new files at this location should create it first - see QDir::mkpath()."
    See "offlineStoragePath":http://qt-project.org/doc/qt-5.0/qtqml/qqmlengine.html#offlineStoragePath-prop



  • I slightly modified code (database & ini file are presented in "assets:" files system. Path: "assets:/qml/Project/databases").

    So:
    @
    void QtQuick2ApplicationViewer::setLocalStoragePath(const QString &path)
    {
    engine()->setOfflineStoragePath(QString("assets:/") + QtQuick2ApplicationViewerPrivate::adjustPath(path));
    }
    @

    But db didn't opened, when i try to open it from js. It just creates the new one.



  • Solved. Assets file system seems to be readonly.

    So:
    @
    #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_NO_SDK)
    QString dbPath = QString("assets:/") + QtQuick2ApplicationViewerPrivate::adjustPath(path);
    QString hash = QString("c32516babc5b6c47eb8ce1bfc223253c");
    QDir dir(QString("."));
    dir.mkpath(QString("./Databases"));
    QFile dbFile(dbPath + QString("/Databases/") + hash + QString(".sqlite"));
    if (dbFile.exists()) {
    QString newDbName = QString("./Databases/") + hash + QString(".sqlite");
    dbFile.copy(newDbName);
    QFile::setPermissions(newDbName, QFile::WriteOwner | QFile::ReadOwner);
    }
    QFile iniFile(QString("./Databases/") + hash + QString(".ini"));
    iniFile.open(QIODevice::WriteOnly);
    iniFile.write("[General]\nDescription=Catalog\nDriver=QSQLITE\nName=Catalog\nVersion=1.0");
    iniFile.close();
    engine()->setOfflineStoragePath(QLatin1String("."));
    #else
    engine()->setOfflineStoragePath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
    #endif
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.