admin管理员组文章数量:1406039
Now I'm currently developing a password manager program. I have some encrypted .db file, after decrypting it as QByteArray
I want to save it somewhere in RAM so I don't create temporary files. After it I need to load it to QSqlDatabase
.
I have tried this:
QFile *f;
f = new QFile(":memory:");
f->open(QIODevice::WriteOnly);
f->write(decrypted);
f->close();
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();
QSqlQuery query(db);
query.exec("SELECT * FROM data");
while(query.next())
{
qDebug() << query.value(0).toString();
}
And got this error:
QIODevice::write (QFile, ":memory:"): device not open
How can I do that?
Now I'm currently developing a password manager program. I have some encrypted .db file, after decrypting it as QByteArray
I want to save it somewhere in RAM so I don't create temporary files. After it I need to load it to QSqlDatabase
.
I have tried this:
QFile *f;
f = new QFile(":memory:");
f->open(QIODevice::WriteOnly);
f->write(decrypted);
f->close();
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();
QSqlQuery query(db);
query.exec("SELECT * FROM data");
while(query.next())
{
qDebug() << query.value(0).toString();
}
And got this error:
QIODevice::write (QFile, ":memory:"): device not open
How can I do that?
Share Improve this question edited Mar 6 at 15:59 wohlstad 30.2k17 gold badges61 silver badges94 bronze badges asked Mar 6 at 15:43 Olexandr RadchenkoOlexandr Radchenko 113 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 0I have used solution of musicamante, thank you a lot!
Firstly I have to decrypt encrypted database to a QByteArray
, then I writing decrypted data to aQTemporaryFile
.
Next, copying database content that file to a QSqlDatabase
stored in :memory:
. After that, I overwrite temporary file`s content by zeroes(to make deletiong more safer) and deleting it.
If I want to write back changed data I need to create QTemporaryFile
and copy :memory:
database to it, afterwad copy content of file to QByteArray
, ecnrypting it and writing to encrypted database file.
本文标签: cHow to load QByteArray to RAM and access it as db fileStack Overflow
版权声明:本文标题:c++ - How to load QByteArray to RAM and access it as .db file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965141a2634898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
f->open()
is ignored and the error atf->write()
is expected. – 3CxEZiVlQ Commented Mar 6 at 16:01:memory:
is a sqlite concept not something you can use with QFile – drescherjm Commented Mar 6 at 17:27QFile(":memory:");
is completely inappropriate. As written above, it's a sqlite concept, not a real path. The result of the above would be to try to open a physical file named ":memory:" in the current working dir. Even assuming that it would work (and it won't at least on Windows, for which the colon is an illegal character for file or directory names), then it would obviously fail your requirement of a memory object. Also, as thesetDatabaseName()
docs explain, when using:memory:
with the SQLite driver it will "create a temporary database", so it's inappropriate anyways. – musicamante Commented Mar 6 at 20:42sqlite3_deserialize
can create a SQLite connection on an in-memory blob of data (usually obtained from a previous call tosqlite3_serialize
. Here's an example of diggingsqlite3*
handle out ofQSqlDatabase
object. – Igor Tandetnik Commented Mar 6 at 22:12