Re: libpq (C++) - Insert binary data - Mailing list pgsql-general

From Christian Ullrich
Subject Re: libpq (C++) - Insert binary data
Date
Msg-id i83gj9$b7h$1@dough.gmane.org
Whole thread Raw
In response to Re: libpq (C++) - Insert binary data  (GOO Creations <goocreations@gmail.com>)
List pgsql-general
* GOO Creations wrote:

> This is what I have until now to insert data
>
> char *query = "insert into table1 (bytes) values ($1)";
> QByteArray chip = <assignment of bytes>;
> const char *data = chip->data();
> const char* params[]={data};
> const int params_length[]={chip->length()};
> const int params_format[]={1};
> result = PQexecParams(mDatabase, query, 1, in_oid, params,
> params_length, params_format, 0);
>
> The first problem I have is that I'm not sure if const int
> params_length[]={chip->length()}; is the correct way to provide the
> length. Second of all, is this actually the correct way of doing it,
> isn't there a beter way?

It is certainly the simplest way of doing it in plain libpq, as long as
you're using the binary format (which you are doing here). According to
the documentation, QByteArray::length() returns the number of bytes in
the array, so it is the correct size.

I'm not sure if the (internal) binary format of bytea is guaranteed not
to change in future versions of PostgreSQL. Currently, as you obviously
found out yourself, it's pretty simple -- no transformation at all, the
binary format is just the data.

To make sure that your application supports any changes in future
versions, you should consider using the "hex" text format instead. See
section 8.4.1 of the manual for version 9.0.

--
Christian

pgsql-general by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: PgWest 2010 talk descriptions are up
Next
From: GOO Creations
Date:
Subject: Re: libpq (C++) - Insert binary data