2

I'm trying to insert binary data (QImage) into a PostgreSQL 8.4.9 bytea column from Qt 4.8. My code looks like this:

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();

This works, but I get the following error:

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'\'

How can I escape the data properly to avoid this warning?

EDIT :

Here is some essential information regarding this topic: http://www.postgresql.org/docs/8.4/static/datatype-binary.html

The way I see it, each byte should be surrounded by E''::bytea before passing to bindValue. How can I accomplish this?

Marek Miettinen
  • 399
  • 1
  • 7
  • 18
  • 1
    I'm not sure what are you trying to do by supplying a binary data where a column name should go (the first mention of `:image` in your INSERT statement). – Milen A. Radev Feb 15 '12 at 13:13
  • That was just a typo. I cleaned columns not related to this problem from the code to make the question more general. – Marek Miettinen Feb 15 '12 at 18:07

2 Answers2

3

Have a look at the docs for .bindValue - you most probably need to indicate that the value is binary:

query.bindValue(":image", ba, QSql::In | QSql::Binary);
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
0

Have you tried to use the E'' around your QByteArray? I don't know if that will work for an image but it seems to work for strings.

String literals and escape characters in postgresql

Community
  • 1
  • 1
masebase
  • 4,915
  • 3
  • 24
  • 20