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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965142a4603630.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