Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* 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


--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux