X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4D8DA534B8 for ; Sat, 14 May 2005 15:37:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33068-05 for ; Sat, 14 May 2005 18:36:57 +0000 (GMT) Received: from gateway.wintersgift.com (unknown [209.121.79.85]) by svr1.postgresql.org (Postfix) with ESMTP id C217852A62 for ; Sat, 14 May 2005 15:36:56 -0300 (ADT) Received: from [192.168.1.103] (unknown [209.121.79.84]) by gateway.wintersgift.com (Postfix) with ESMTP id B5957167767 for ; Sat, 14 May 2005 11:36:51 -0700 (PDT) Mime-Version: 1.0 (Apple Message framework v622) To: pgsql-interfaces@postgresql.org Message-Id: <4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com> Content-Type: multipart/alternative; boundary=Apple-Mail-1-349856610 From: Teunis Peters Subject: Two seperate problems with libpq - arrays and bytea edit Date: Sat, 14 May 2005 11:40:10 -0700 X-Mailer: Apple Mail (2.622) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.029 tagged_above=0 required=5 tests=LOTS_OF_STUFF X-Spam-Level: X-Archive-Number: 200505/35 X-Sequence-Number: 5538 --Apple-Mail-1-349856610 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed 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]; } --Apple-Mail-1-349856610 Content-Transfer-Encoding: 7bit Content-Type: text/enriched; charset=US-ASCII 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, 8989,1313,1515"%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], 8989,1313,1515"UPDATE images SET image = $2 WHERE id = $1;", np, 7676,0F0F,5050NULL, paramValues, paramLen, 7676,0F0F,5050NULL, 0000,0000,FFFF1); 7676,0F0F,5050if (PQresultStatus(r) != PGRES_COMMAND_OK) { NSLog(8989,1313,1515@"UPDATE failed: %s", PQerrorMessage([conn conn])); } PQclear(r); access (subset of full code) sprintf(buf, 8989,1313,1515"%i", cat_id); paramValues[0000,0000,FFFF0] = buf; res = [db_result alloc]; [res init:PQexecParams([conn conn], 8989,1313,1515"SELECT image FROM images WHERE id = $1",0000,0000,FFFF1,7676,0F0F,5050NULL,paramValues,7676,0F0F,5050NULL,7676,0F0F,5050NULL,0000,0000,FFFF1)]; 7676,0F0F,5050if (!res) 7676,0F0F,5050return 7676,0F0F,5050nil; 7676,0F0F,5050if ([res ntuples] << 0000,0000,FFFF1) 7676,0F0F,5050return 7676,0F0F,5050nil; { 7676,0F0F,5050char* ptr; 7676,0F0F,5050char* decoded; size_t i, decodedlen; NSString* ns; ptr = [res getValue:0000,0000,FFFF0 field:0000,0000,FFFF0]; 7676,0F0F,5050if (ptr != 7676,0F0F,5050NULL) { i = [res getlength:0000,0000,FFFF0 field:0000,0000,FFFF0]; 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:0000,0000,FFFE0 field:0000,0000,FFFE0]; 7675,0F0E,504Fif (ptr != 7675,0F0E,504FNULL) { i = [res getlength:0000,0000,FFFE0 field:0000,0000,FFFE0]; image = [[NSData dataWithBytes:decoded length:i] retain]; } --Apple-Mail-1-349856610--