1

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?

9
  • 2
    you are going in the wrong direction, sqlite already has an optional password protection system, see stackoverflow.com/a/54301651/15649230 Commented Mar 6 at 15:48
  • 1
    The result of f->open() is ignored and the error at f->write() is expected. Commented Mar 6 at 16:01
  • 2
    :memory: is a sqlite concept not something you can use with QFile 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. 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. Commented Mar 6 at 22:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.