Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Local storage working on Desktop but not in Android
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 3 Posters 2.5k Views 1 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.
  • Z Offline
    Z Offline
    zulu_x26
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      coursar
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zulu_x26
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          coursar
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zulu_x26
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zulu_x26
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                Hi,

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

                157

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  zulu_x26
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

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

                    157

                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      zulu_x26
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #11

                        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.

                        157

                        1 Reply Last reply
                        0

                        • Login

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