SQLite database file within qrc



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



  • 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");



  • @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


  • Lifetime Qt Champion

    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


Log in to reply
 

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