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. Qt how connect to an existing SQLlite file in iOS ?
Forum Updated to NodeBB v4.3 + New Features

Qt how connect to an existing SQLlite file in iOS ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 3 Posters 733 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.
  • O Offline
    O Offline
    OanhBK
    wrote on 14 Jan 2021, 09:44 last edited by
    #1

    i read the database. this code works on android device, but on iOS device this code don't work
    file .h


    QFile DbFile;
    QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
    DbFile.setFileName("assets:/SUN_SHINE_LED.db");
    DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
    QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
    QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
    if(!db.open())
    {
    qDebug()<<"\nfail!!!";
    return;
    }


    file .pro


    QT += quick
    QT += core gui
    QT +=sql
    QT +=network
    QTPLUGIN+=qsqlite

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
    deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/android
    QMAKE_BUNDLE_DATA+=deployment

    android
    {
    my_files.path = /assets
    my_files.files = $$PWD/android/*
    INSTALLS += my_files
    }


    I used the folder ''android/assets' in 'Other files' in the project. I kept the SUN_SHINE_LED.db file there

    J 1 Reply Last reply 14 Jan 2021, 10:19
    0
    • O OanhBK
      14 Jan 2021, 09:44

      i read the database. this code works on android device, but on iOS device this code don't work
      file .h


      QFile DbFile;
      QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
      DbFile.setFileName("assets:/SUN_SHINE_LED.db");
      DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
      QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
      QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
      QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");
      if(!db.open())
      {
      qDebug()<<"\nfail!!!";
      return;
      }


      file .pro


      QT += quick
      QT += core gui
      QT +=sql
      QT +=network
      QTPLUGIN+=qsqlite

      ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
      deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/android
      QMAKE_BUNDLE_DATA+=deployment

      android
      {
      my_files.path = /assets
      my_files.files = $$PWD/android/*
      INSTALLS += my_files
      }


      I used the folder ''android/assets' in 'Other files' in the project. I kept the SUN_SHINE_LED.db file there

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Jan 2021, 10:19 last edited by
      #2

      @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

      QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");

      How should this work? You only provide the file name but not whole path.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 14 Jan 2021, 10:23
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 14 Jan 2021, 10:20 last edited by
        #3

        Hi and welcome to devnet,

        AFAIK, the "assets:/" virtual file system is only available on Android as it is a feature only available on that platform. You have to put your original database somewhere else for iOS. You can find how to bundle data in the platform notes. Therefore, you can put it in the bundled data and copy it from there to the correct place.

        In any case, you should add some error checks also for your copy operation for cases like this one.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        O 1 Reply Last reply 14 Jan 2021, 10:46
        1
        • J jsulm
          14 Jan 2021, 10:19

          @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

          QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");

          How should this work? You only provide the file name but not whole path.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Jan 2021, 10:23 last edited by
          #4

          @jsulm said in Qt how connect to an existing SQLlite file in iOS ?:

          @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

          QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");

          How should this work? You only provide the file name but not whole path.

          SQLite does not care if the file exists or not. If it does not, it will create it. Nothing Qt specific here. But it's indeed surprising that it succeed on mobile since the application are AFAIK, in a readonly storage.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 14 Jan 2021, 12:56
          0
          • S SGaist
            14 Jan 2021, 10:20

            Hi and welcome to devnet,

            AFAIK, the "assets:/" virtual file system is only available on Android as it is a feature only available on that platform. You have to put your original database somewhere else for iOS. You can find how to bundle data in the platform notes. Therefore, you can put it in the bundled data and copy it from there to the correct place.

            In any case, you should add some error checks also for your copy operation for cases like this one.

            O Offline
            O Offline
            OanhBK
            wrote on 14 Jan 2021, 10:46 last edited by
            #5

            @SGaist I created the folder "ios" in the project and kept the SUN_SHINE_LED.db file there. But code don't work :(
            file .pro


            QT += quick
            QT += core gui
            QT +=sql
            QT +=network
            QTPLUGIN+=qsqlite
            deployment.files=$$PWD/$$IOS_PACKAGE_SOURCE_DIR/ios
            QMAKE_BUNDLE_DATA+=deployment
            ios
            {
            my_files.files = $$PWD/ios/*
            INSTALLS += my_files
            }


            file .h


            QFile DbFile;
            QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
            DbFile.setFileName("/SUN_SHINE_LED.db");
            DbFile.copy(DatabaseDataStoragePath + "/SUN_SHINE_LED.db");
            QFile::setPermissions(DatabaseDataStoragePath + "/SUN_SHINE_LED.db", QFile::WriteOwner | QFile::ReadOwner);
            db=QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName("SUN_SHINE_LED.db");
            if(!db.open())
            {
            qDebug()<<"\nfail!!!";
            return;
            }

            1 Reply Last reply
            0
            • S SGaist
              14 Jan 2021, 10:23

              @jsulm said in Qt how connect to an existing SQLlite file in iOS ?:

              @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

              QSqlDatabase db.setDatabaseName("SUN_SHINE_LED.db");

              How should this work? You only provide the file name but not whole path.

              SQLite does not care if the file exists or not. If it does not, it will create it. Nothing Qt specific here. But it's indeed surprising that it succeed on mobile since the application are AFAIK, in a readonly storage.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Jan 2021, 12:56 last edited by
              #6

              @SGaist Sure, but it will be a new file, not the one OP wants to use.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 14 Jan 2021, 14:02
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 Jan 2021, 14:01 last edited by
                #7

                @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

                DbFile.setFileName("/SUN_SHINE_LED.db");

                I am 100% sure that you do not have access to the "/" path on iOS.

                Use the proper path within the bundle. Your application and the bundled data are not in the same folder.

                @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

                db.setDatabaseName("SUN_SHINE_LED.db");

                This is a relative path, use the full path to where you copied the file.

                And again: add a check that the copy succeeded.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                O 1 Reply Last reply 15 Jan 2021, 09:34
                0
                • J jsulm
                  14 Jan 2021, 12:56

                  @SGaist Sure, but it will be a new file, not the one OP wants to use.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 14 Jan 2021, 14:02 last edited by
                  #8

                  @jsulm said in Qt how connect to an existing SQLlite file in iOS ?:

                  @SGaist Sure, but it will be a new file, not the one OP wants to use.

                  Agreed, just explaining why you usually can succeed in opening a SQLite database even if the file does not exists.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S SGaist
                    14 Jan 2021, 14:01

                    @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

                    DbFile.setFileName("/SUN_SHINE_LED.db");

                    I am 100% sure that you do not have access to the "/" path on iOS.

                    Use the proper path within the bundle. Your application and the bundled data are not in the same folder.

                    @OanhBK said in Qt how connect to an existing SQLlite file in iOS ?:

                    db.setDatabaseName("SUN_SHINE_LED.db");

                    This is a relative path, use the full path to where you copied the file.

                    And again: add a check that the copy succeeded.

                    O Offline
                    O Offline
                    OanhBK
                    wrote on 15 Jan 2021, 09:34 last edited by
                    #9

                    @SGaist great!!! Thank you so much !

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 15 Jan 2021, 18:57 last edited by
                      #10

                      You're welcome !

                      Do you got it to work ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      1/10

                      14 Jan 2021, 09:44

                      • Login

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