From: | John McKown <john(dot)archie(dot)mckown(at)gmail(dot)com> |
---|---|
To: | Poul Kristensen <bcc5226(at)gmail(dot)com>, PostgreSQL General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used |
Date: | 2016-11-21 23:48:42 |
Message-ID: | CAAJSdji024ecwvCreTviPpO4KPGTw=AB=yPe1cE=NtvmTcKDMA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Mon, Nov 21, 2016 at 11:22 AM, Poul Kristensen <bcc5226(at)gmail(dot)com> wrote:
> Thank you for fast repons!
>
> The $1 substitution below. I assume that it refers to "joe's place". But
> it is not very clear to me, how "joe's place" will appear instead of $1
> when running. Where is it possiible to read more about this? There just
> is'nt much about substitution in C online. Any recommended books to buy?
>
>
> /* Here is our out-of-line parameter value */
> paramValues[0] = "joe's place";
>
> res = PQexecParams(conn,
> "SELECT * FROM test1 WHERE t = $1",
> 1, /* one param */
> NULL, /* let the backend deduce param type */
> paramValues,
> NULL, /* don't need param lengths since text */
> NULL, /* default to all text params */
> 1); /* ask for binary results */
> }
>
> /Poul
>
>
>
It is described better here:
https://www.postgresql.org/docs/9.6/static/libpq-exec.html
than I can do. But I just noticed a mistake in your code, or maybe just
something left out. I would say:
char *value1 = "joe's place";
char **paramV
a
lues = &value1;
/* closer match to the documentation's syntax */
//char *paramValues[] = {"joe's place"}; /* same as above, different syntax
*/
//
//char *paramValues[1]; /* this looks to be missing */
//paramValues[0]="joe's place"; /* what you had */
res = PQexecParms(conn,
"SELECT * FROM test1 WHERE t = $1",
1, /* there is only 1 entry in paramValues array */
paramValues, /* address of parameter value array */
NULL, /* don't need param lengths since text */
NULL, /* defaul to all text params */
1); /* return all values as binary */
Well, you have an array of pointers to characters called paramValues. The
$1 refers to whatever is pointed to by paramValues[0], which is a pointer
to value1 which is a C "string". Basically in the second parameter, the
command, the $n is used as a 1-based index into the paramValues[] array.
This means that the actual C language array value is one less (since C
arrays are 0-based). Which means that "$n" (n>=1) in the "command" string
refers to value pointed to by paramValues[n-1]. The 3rd value, 1 in this
case, tells PQexecParms how many entries there are in the paramValues[]
array. I guess this is a type of validity check that the $n in the command
string is not too large for the array.
Note: please keep the discussion on the list, not to me personally. It may
be of help to others (or maybe not, I don't know.)
--
Heisenberg may have been here.
Unicode: http://xkcd.com/1726/
Maranatha! <><
John McKown
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2016-11-22 01:12:22 | Re: variable value in array_to_string |
Previous Message | Fran ... | 2016-11-21 23:34:33 | Re: Database migration to RDS issues permissions |