Is there any Qt SQLite plugin for storing database in RAM by VFS (for loading database from Qt resource file)?

半世苍凉 提交于 2019-12-01 04:51:19

You could take advantage of Qt QTemporaryFile to copy your data and start. QTemporaryfile works on every os.

Here is an example (this temporary file is associated to the entire qApp so that it will be removed once you quit the application):

QTemporaryFile tmpFile(qApp);
tmpFile.setFileTemplate("XXXXXX.sqlite3");
if (tmpFile.open()) {
    QString tmp_filename=tmpFile.fileName();
    qDebug() << "temporary" << tmp_filename;

    QFile file(":/test.sqlite3");
    if (file.open(QIODevice::ReadOnly)) {
        tmpFile.write(file.readAll());
    }

    tmpFile.close();
}

The you can reopen the file tmpFile.fileName() as QSqlDatabase:

QSqlDatabase::setDatabase(tmpFile.fileName());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!