Re: pass date type data to PQexecparams

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: pr0v4 <josip(dot)povreslo(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: pass date type data to PQexecparams
Date: 2006-09-26 14:35:30
Message-ID: 20060926143530.GA802@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Tue, Sep 26, 2006 at 10:28:08AM +0200, pr0v4 wrote:
> On 26/09/06, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> > Please post a simple but complete program that shows what you're
> > doing.
>
> Ok, I've created sample code just like you said.
> So here it is:

Please copy the mailing list on replies. This gives other people
an opportunity to respond, which can get you an answer faster if
the person you emailed privately is unavailable. It also allows
others who are reading the list now or in the future via the archives
to learn from the discussion if they have similar questions.

The code you sent doesn't compile due to several problems like
undeclared variables and syntax errors. After those errors are
corrected and the program compiles, it fails at runtime for various
reasons. These difficulties prevent people from running the program
to see what's happening without spending additional time to fix the
mistakes, by which time the code might no longer resemble what
you're really doing, which reduces its usefulness as a test case
(not to mention that a person who had been willing to help might
have given up by now). When posting a test program, please compile
and run it first to verify that it "works" in the sense that it
runs and demonstrates the behavior you're seeing.

All that said, the main problem is here:

> bindate = htonl( (uint32_t) *date);
> paramValues[0] = (char*) &bindate;
> paramLengths[0] = sizeof(bindate);
> paramFormats[0] = 1;

The date variable is a char * that points to a date value in text
format. That is, date points to memory that contains the characters
in a string like "2006-09-26" followed by a NUL (\0) character.
The values in memory would look something like this (in hex):

32 30 30 36 2d 30 39 2d 32 36 00

The bindate assignment takes the first character (0x32), casts it
to uint32_t, then converts that value to network byte order. The
result has nothing to do with the original date value. Try this
instead:

paramValues[0] = date;
paramLengths[0] = strlen(date);
paramFormats[0] = 0;

Since the date value is in text format you can use strlen() to get
its length; you could also use PQgetlength() with the result pointer
since you fetched the value from a query. The format should be 0
(zero) because the value is in text format, not binary format.

--
Michael Fuhr

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Christopher Bland 2006-09-26 19:12:37 Inserting & Retrieving Unicode with libpq
Previous Message Oliver Fromme 2006-09-26 13:15:27 Re: Multiple UNIX-domain sockets?