Re: libpq (C++) - Insert binary data
От | Christian Ullrich |
---|---|
Тема | Re: libpq (C++) - Insert binary data |
Дата | |
Msg-id | i83gj9$b7h$1@dough.gmane.org обсуждение исходный текст |
Ответ на | Re: libpq (C++) - Insert binary data (GOO Creations <goocreations@gmail.com>) |
Список | 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 по дате отправления: