[SOLVED] Local storage working on Desktop but not in Android



  • I am developing my first application in Qt Quick and I am using local storage to store data. When used on desktop (ubuntu) it works perfectly. Deployed in Android though, it stores the data only as long as the app is open. If I close the app and open it again, all data is lost. I'm really puzzled by this behavior and I have no clue where to start looking for the problem, so any help would be appreciated.



  • Which version of qt you're using? Are you set offlineStoragePath?



  • I am using Qt 5.3 and I didn't change offlineStoragePath.



  • How are you open and init database? Can you give some code or sample project where problem exists? I've used localstorage with Android, iOS on 5.3 and 5.3.1 and it works as expected.



  • Unfortunately today I'm on the road and I don't know when I'll have access to the computer with the application, but I'll post the relevant code as soon as possible.



  • Ok, here goes the code:

    I have a global variable:
    @var _db = null;@

    I open the database with:
    @function openDB()
    {
    _db = Sql.LocalStorage.openDatabaseSync("myScores", "1.0", "", 1000000, createTables);
    }@

    And createTables is (I trimmed some of the SQLs to keep the post shorter):
    @function createTables(db)
    {
    db.transaction(
    function(tx){
    tx.executeSql("CREATE TABLE IF NOT EXISTS songs ...");
    tx.executeSql("CREATE TABLE IF NOT EXISTS notes ...");
    }
    );
    }@

    And then I have several functions to read/write which follow the same pattern:

    @function insertNewSong(songName)
    {
    _db.transaction(
    function(tx) {
    tx.executeSql("INSERT INTO songs (title) VALUES (?)", [songName]);
    var results = tx.executeSql("SELECT MAX(id) AS mid FROM songs");
    functionResult = results.rows.item(0).mid;
    }
    );
    }@


  • Moderators

    Hi,

    I think you need to set permissions to your database file first on Android.



  • Can you please tell me what permissions would be required? I tried READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE but there is no change. The data stays there only until I close the app. When I open it again, all data is gone.


  • Moderators

    I'm not sure if th following applies when done through the js api's
    I had to do this when copying database during first run of app
    @
    QString dbfilepath = QDir::currentPath()+"/Scores.db";
    if(!QFile::exists(dbfilepath))
    {
    QFile::copy(":/db/db/Scores.db", QDir::currentPath()+"/Scores.db");
    QFile::setPermissions(QDir::currentPath()+"/Scores.db",QFile::ReadOwner|QFile::WriteOwner);
    }
    @



  • Ok, I've got it figured out. It's quite silly, but I'll post it, maybe it will help someone.

    The problem is that, for whatever reason, the database version doesn't get saved in the ini file and it remains blank. So when I closed the application and opened it again calling openDatabaseSync with version "1.0" made everything stop, because the version was not the same. Changing the database version in the code to blank solved it.
    The desktop version was working because at some point I had manually modified the ini file (and forgot about it).

    I want to thank everyone who took their time and tried to help me.


  • Moderators

    That's great. It was a good effort from you.
    You can mark the thread as solved by editing the thread title and prepend [solved] so that other's may know that this thread has a possible solution.


Log in to reply
 

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