Re: Variable array sizes with PQexecParams - Mailing list pgsql-novice

From Tom Lane
Subject Re: Variable array sizes with PQexecParams
Date
Msg-id 22631.1151353944@sss.pgh.pa.us
Whole thread Raw
In response to Variable array sizes with PQexecParams  ("Garcia, Joshua" <Joshua.Garcia@xerox.com>)
Responses Re: Variable array sizes with PQexecParams  ("Garcia, Joshua" <Joshua.Garcia@xerox.com>)
List pgsql-novice
"Garcia, Joshua" <Joshua.Garcia@xerox.com> writes:
> I want to use PQexecParams to insert into a field that takes an array of
> varchars.  I tried something like:
> INSERT INTO table1(column1) VALUES ('{$1,$2}')
> But, this just inserts {$1,$2} into the field.

Well, yeah.  If it did anything else that'd be a catastrophic bug.

Try something like

INSERT INTO table1(column1) VALUES (ARRAY[$1,$2])

For varchar this will probably work as-is, for other datatypes you might
need to add explicit casts to determine the array element types, eg

INSERT INTO table1(column1) VALUES (ARRAY[$1::integer,$2::integer])

Another possibility is to treat the whole array value as one parameter:

INSERT INTO table1(column1) VALUES ($1)

where the value of $1 is like "{foo,bar}", but this gets into having to
quote the data values correctly to make them valid array elements.  The
first way is probably safer.

            regards, tom lane

pgsql-novice by date:

Previous
From: Richard Broersma Jr
Date:
Subject: Re: Table Merge Successful, Primary Keys Missing
Next
From: "Garcia, Joshua"
Date:
Subject: Re: Variable array sizes with PQexecParams