Re: libpq (C++) - Insert binary data

From: Christian Ullrich <chris(at)chrullrich(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: libpq (C++) - Insert binary data
Date: 2010-10-01 02:21:33
Message-ID: i83gj9$b7h$1@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message GOO Creations 2010-10-01 06:58:03 Re: libpq (C++) - Insert binary data
Previous Message Greg Smith 2010-09-30 23:58:36 Re: ECPG - Some errno definitions don't match to the manual