参考:How to Store and Retrieve Image on SQLite
sqlite的BLOB类型对应于QT的QByteArray类型。想将数据以二进制的形式存储在sqlite中,则需要先将数据转成QByteArray。比如存储QString类型数据:
QString strValue = "I'm zqykj!";
QByteArray ba;
QDataStream out(&ba,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);
out << strValue;
通过bindValue的形式将数据写入数据库:
QByteArray ba;
QsqlQuery query;
QString strsql = "update demo set value = :value where name = :name;";
query.prepare(strsql);
query.bindValue(":value",ba);
query.bindValue(":name","zqykj");
query.exec();
读取二进制数据转换为QString:
QString strValue;
QByteArray ba;
QDataStream in(&ba,QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_5_5);
in >> strValue;
原文链接:https://www.f2er.com/sqlite/198256.html