From: | Teunis Peters <teunis(at)wintersgift(dot)com> |
---|---|
To: | pgsql-interfaces(at)postgresql(dot)org |
Subject: | Two seperate problems with libpq - arrays and bytea edit |
Date: | 2005-05-14 18:40:10 |
Message-ID: | 4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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];
}
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2005-05-14 18:51:13 | Re: Two seperate problems with libpq - arrays and bytea edit |
Previous Message | Bruce Momjian | 2005-05-13 18:23:31 | Re: bcc32.mak for libpq broken? (distro 8.0.0) |