Nominate our 2022 Qt Champions!

Adding db to resources

  • hi to every one
    i want to know can i add my qsqlite database that it is near of my program to my resources?
    if it can't done.can i change its path to another folder , because when i use setdatabasename function i cant set its path and it only searches application dir path
    i want to be :
    app dir path/Data/1.db

  • You can not put a database file into the Qt resources (qrc). The reason is simply that Qt uses the unmodified sqlite library and passes the path to the database file to sqlite. And as sqlite is not aware of Qt at all and its resource system, it cannot find the file.

    Additionally, even if it would work, the database in the resources would be read only!

    What you can do is: Store the database in the resources and before using it the first time copy it to the real world file system. QFile is aware of the resources and can copy between those and the file system. Just make sure that you do not overwrite a modified database of the file system copy with the resource version.

  • can i move it to another directory?

  • Sure, you can put it anywhere you have write rights on the file system.

  • but in which function can i set its address?because setdatabasename function only sets a db name

  • How do you figure that? Set database name takes a complete path for the SQLite driver.

  • In case of SQLite - and only there - setDatabaseName() takes the full path to the database file. It may be a single file name, in that case it is located in "the current directory", or some relative or absolute path + file name.

    PS: I've added a "doc note":/doc/qt-4.8/qsqldatabase.html#notes

  • Actually, the same goes for the Borland Interbase (QIBASE) driver: it also takes a full file path. And I wrote an MSACCESS driver that did the same, but that one is not in Qt, so it does not count ;-)

  • Good to know, I've updated the note.

Log in to reply