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

Now I'm currently developing a password manager program. I have some encrypted .db file, after dec

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965142a4603630.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信