我正在尝试将二进制数据 (QImage
) 从 Qt 4.8 插入到 PostgreSQL 8.4.9 bytea
列中。
我的代码如下所示:
QImage image;
QByteArray ba;
QBuffer buffer(&ba);
image.save(&buffer, "PNG"); // Save the QImage data into the QBuffer
QSqlQuery query = QSqlQuery(database);
query.prepare("INSERT INTO images (image) "
"VALUES (:image)");
query.bindValue(":image", ba);
query.exec();
qDebug() << query.lastError().text();
这有效,但我收到以下错误:
WARNING: nonstandard use of \ in a string literal LINE 1: ...XECUTE
qpsqlpstmt_1 ('\211PNG... HINT: Use the escape string syntax for
backslashes, e.g., E'\'
如何正确转义数据以避免此警告?
编辑:
以下是有关此主题的一些基本信息: http://www.postgresql.org/docs/8.4/static/datatype-binary.html
在我看来,每个字节在传递给 bindValue
之前都应该被 E''::bytea
包围。我怎样才能做到这一点?
最佳答案
看看 docs对于 .bindValue
- 您很可能需要 indicate该值是二进制的:
query.bindValue(":image", ba, QSql::In | QSql::Binary);
关于c++ - 如何使用 Qt 转义二进制数据以插入 PostgreSQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293387/