PDA

View Full Version : QSqlDatabase and Postgres - Insert bytes into database



goocreations
12th September 2010, 18:15
I have a Postgres database with a BYTEA field.
I want to insert bytes into this fields.

I'm currently doing this:


QByteArray data = <get data>;
QString query = "INSERT INTO images (id, bytes) VALUES (1, :data)";
QSqlQuery sql(query);
sql.bindValue(":data", data);
sql.exec();

This failes and I get the following error:


ERROR: syntax error at or near "("
LINE 1: EXECUTE ('\\000\\000\\000\\000\\000\\000\\000\\000\\000\\0 0...


If I replace the :data with NULL, everything works fine.

Does anyone know how to insert binary dat into Postgres using QSqlDatabase and QSqlQuery.
Any reference to an online tutorial will also be greate, because I can't find one.

Thank you

Lykurg
12th September 2010, 18:54
When I remember right it's a "problem" of the driver. You have to convert it to base64 before inserting:
sql.bindValue(":data", data.toBase64());