pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedTwo seperate problems with libpq - arrays and bytea edit
2+ messages / 2 participants
[nested] [flat]
* Two seperate problems with libpq - arrays and bytea edit
@ 2005-05-14 18:40 Teunis Peters <teunis@wintersgift.com>
2005-05-14 18:51 ` Re: Two seperate problems with libpq - arrays and bytea edit Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Teunis Peters @ 2005-05-14 18:40 UTC (permalink / raw)
To: pgsql-interfaces
I'm trying to figure out how to read from and write to bytea values and
array (specifically "text[2][]" for a key:value 'dictionary') and
cannot figure it out. Arrays I'm completely lost - I haven't a clue
what the format would be.
bytea - my code segmentation faults. PQgetlength returns 0. I don't
know if I'm putting it in the database correctly.
some sample code. Yes it's in Objective C. [db_result] embeds a
"PGresult" value and does a lot of error checking. image is of type
NSData which basically holds binary data. The rest SHOULD be clear.
I'd provide a smaller example except I'm not yet sure how to create
console apps in MacOSX... I suppose I could do it in linux though.
Any advice helpful. I've been stewing my brain on this for a while
now and am on something of a time limit.
getValue == PQgetvalue(res,tupple,field)
getlength == PQgetlength(res,tupple,field)
I hope that helps.
commit (subset of full code)
sprintf(buf, "%i", cat_id);
paramValues[np] = buf; paramLen[np] = strlen(buf); np++;
paramValues[np] = [image bytes]; paramLen[np] = [image length]; np++;
PGresult* r;
r = PQexecParams([conn conn], "UPDATE images SET image = $2 WHERE id =
$1;",
np, NULL, paramValues, paramLen, NULL, 1);
if (PQresultStatus(r) != PGRES_COMMAND_OK) {
NSLog(@"UPDATE failed: %s", PQerrorMessage([conn conn]));
}
PQclear(r);
access (subset of full code)
sprintf(buf, "%i", cat_id);
paramValues[0] = buf;
res = [db_result alloc];
[res init:PQexecParams([conn conn], "SELECT image FROM images WHERE id
= $1",1,NULL,paramValues,NULL,NULL,1)];
if (!res) return nil;
if ([res ntuples] < 1) return nil;
{
char* ptr;
char* decoded;
size_t i, decodedlen;
NSString* ns;
ptr = [res getValue:0 field:0];
if (ptr != NULL) {
i = [res getlength:0 field:0];
decoded = PQunescapeBytea(ptr, &decodedlen);
image = [[NSData dataWithBytes:decoded length:decodedlen] retain];
PQfreemem(decoded);
}
}
Do I access bytea ("image" is a bytea type) this way, or this way:
ptr = [res getValue:0 field:0];
if (ptr != NULL) {
i = [res getlength:0 field:0];
image = [[NSData dataWithBytes:decoded length:i] retain];
}
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Two seperate problems with libpq - arrays and bytea edit
2005-05-14 18:40 Two seperate problems with libpq - arrays and bytea edit Teunis Peters <teunis@wintersgift.com>
@ 2005-05-14 18:51 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2005-05-14 18:51 UTC (permalink / raw)
To: Teunis Peters <teunis@wintersgift.com>; +Cc: pgsql-interfaces
Teunis Peters <teunis@wintersgift.com> writes:
> I'm trying to figure out how to read from and write to bytea values and
> array (specifically "text[2][]" for a key:value 'dictionary') and
> cannot figure it out.
Well, since you are asking for binary output format from PQexecParams,
the bytea result is going to just be raw binary data: you should not be
trying to apply PQunescapeBytea to it. I don't know the notation you
are using well enough to tell what's wrong with your second variant.
> Arrays I'm completely lost - I haven't a clue what the format would be.
Looking at array_send, the binary wire format is
number of dimensions (int4)
flags (int4)
element type id (Oid)
for each dimension:
dimension length (int4)
dimension lower subscript bound (int4)
for each array element:
element value, in the appropriate format
The "flags" are currently always 0, and I would recommend punting if
you see something else, since something else would very possibly
indicate a change in the wire format.
Don't forget that the wire format for integers is network byte order
(ie, big-endian).
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-05-14 18:51 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-05-14 18:40 Two seperate problems with libpq - arrays and bytea edit Teunis Peters <teunis@wintersgift.com>
2005-05-14 18:51 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox