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?

2
  • 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). Commented Feb 15, 2012 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. Commented Feb 15, 2012 at 18:07

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't have any effect. And I believe it should be QSql::In | QSql::Binary
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.