pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Teunis Peters <teunis@wintersgift.com>
To: pgsql-interfaces@postgresql.org
Subject: Two seperate problems with libpq - arrays and bytea edit
Date: Sat, 14 May 2005 11:40:10 -0700
Message-ID: <4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com> (raw)

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];
		}

view thread (2+ messages)  latest in thread

Message-ID: <4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com>
Permalink:  ../4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com/
Also on:    postgresql.org/message-id/4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: teunis@wintersgift.com
  Subject: Re: Two seperate problems with libpq - arrays and bytea edit
  In-Reply-To: <4dc6bdd876380e9a6d8673c5d36c449f@wintersgift.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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