c++ - How to load QByteArray to RAM and access it as .db file - Stack Overflow

admin2025-04-18  0

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
  • 2 you are going in the wrong direction, sqlite already has an optional password protection system, see stackoverflow/a/54301651/15649230 – Ahmed AEK Commented Mar 6 at 15:48
  • 1 The result of f->open() is ignored and the error at f->write() is expected. – 3CxEZiVlQ Commented Mar 6 at 16:01
  • 2 :memory: is a sqlite concept not something you can use with QFile – drescherjm Commented Mar 6 at 17:27
  • 1 Using QFile(":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 the setDatabaseName() 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:42
  • 2 sqlite3_deserialize can create a SQLite connection on an in-memory blob of data (usually obtained from a previous call to sqlite3_serialize. Here's an example of digging sqlite3* handle out of QSqlDatabase object. – Igor Tandetnik Commented Mar 6 at 22:12
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744965143a277150.html

最新回复(0)