Store a file in SQLite



We can store each file as binary large object in database.(BLOB).

We need to do in two steps.
    Step 1 : File on the system is read to a QByteArray.
Step 2 : QByteArray is stored as BLOB.

QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return;
QByteArray byteArray = file.readAll();
QSqlQuery query;
query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");
query.addBindValue(byteArray);
query.exec();

QSqlQuery query("SELECT imgdata FROM imgtable");
query.next();
QByteArray array = query.value(0).toByteArray();


   To retrieve :

QSqlQuery query("SELECT imgdata FROM imgtable");
query.next();
QByteArray array = query.value(0).toByteArray();



4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Can u pls show how to display the stored file in widget and to save in a specific path

    ReplyDelete
  4. I need to store the PDF file into the database

    ReplyDelete