Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    SQLite database file within qrc

    General and Desktop
    3
    4
    1945
    Loading More Posts
    • 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.
    • Robey Mardon
      Robey Mardon last edited by

      Is it possible to insert a SQLite .db file in a resource .qrc file?

      1 Reply Last reply Reply Quote 0
      • L
        Leonardo last edited by

        I have never tried, but if it's a read only database, there's no reason for it not to be possible. Just try:

        db.setDatabaseName(":/database.db");

        Robey Mardon 1 Reply Last reply Reply Quote 0
        • Robey Mardon
          Robey Mardon @Leonardo last edited by Robey Mardon

          @Leonardo Unfortunately it doesn't seems to work. I tried the way you said and nothing appears, then I tried using the full path for the database file and worked.

          I just realized that it's not possible:

          SQLite databases cannot be stored in the Qt resource system. You must put it into regular files and distribute them along your application.
          The reason is simple: The database name is just the path to the file. Qt does not intercept the file name but hands it over directly to the SQLite functions (by calling toUtf8() on the “path”), which in turn try to open that file – and will eventually fail, of course.
          Also, keep in mind that a file in a resource is read-only, you would never be able to write to that database.
          goetz

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Technically it can be embedded, BUT you can't make the QtSql module read it even less write it. What you can do however is put a copy of that "embedded" database in a suitable folder and then use it from your application. To get a suitable folder, use QStandardPaths.

            Hope it helps

            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 Reply Quote 1
            • First post
              Last post