Subject: appending alphanumeric string with libPQ

From: John Peter Lee <[email-masked]>
Date: Sun, 11 Jul 1993 19:06:00 -0400 (EDT)

Hi -

I'm running Postgres 4.0.1 on a SPARCstation.

I'm having a problem appending an alphanumeric string (with the numeric part
first) to the backend with libPQ. Here is the scenario: I can create a class
in libPQ:

...

 query_result = PQexec ( "create y87 ( tri_id = char16, ... ); 

...

without a problem.

I'm now building an append query in libPQ like so:

...

 sprintf (buf87, "append y87 ( tri_id = %s, ... )", buf[0], ... ); 
 printf ("\n\nquery is: \n\n%s\n\n", buf87);
 PQexec ( buf87 ); 

...

each item in the array "buf" is a string (character array).
the printf-ed query looks like: 

query is: 

append y87 ( tri_id = 01001HPHDN233MA, ... ) 

**************************************

The parser complains with the following:

WARN:Jul 11 18:15:48:parser: syntax error at or near "HPHDN233MA"

which is part of the first attribute. I then tried to perform a similar append
from the monitor on a test database, and succeeded, using double quotes 
around the attribute like so:

Welcome to the C POSTGRES terminal monitor

Go 
* create y87 (tri_id = char16) \g
Query sent to backend is "create y87 (tri_id = char16) "
CREATE
Go 
* append y87 (tri_id = "01001HPHDN233MA") \g

Query sent to backend is "append y87 (tri_id = "01001HPHDN233MA") "
APPEND 87051

So, I tried putting the quotes into the libPQ version:

query is:

append y87 ( tri_id = "01001HPHDN233MA", ...);

and got the following complaint:

WARN:Jul 11 18:49:41:heap_openr on H failed

********

So, can anyone tell me why this happens, and how can I make it work ???

Thank You,

jp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JP Lee               Institute for Visualization and Perception Research	
[email-masked]                    University of Massachusetts at Lowell 
(508) 934-3384			      1 University Ave. Lowell, MA 01854

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

From: Kai Petzke <[email-masked]>
Date: Mon, 12 Jul 1993 14:10:41 +0200 (MET DST)

Dear,

[some deleted]

>  printf ("\n\nquery is: \n\n%s\n\n", buf87);

I believe, your problem lies here:

	sprintf (buf87, "append y87 ( tri_id = \"%s\", ... )", buf[0], ... ); 

would be correct.  You have to put quotes around a string argument in 
a query.  To put the quotes into the C-string, you have to escape them
with a backslash \.  If the inserted string (your variable buf[0] could
contain quotes, too, you have to write a more sophisticated C programme,
to escape these in the inserted string.

Inserting values like "id233" will work, as far as I know, because the
parser reads them first as a symbol (like class name, field name, etc.),
then converts it to a string, as it sees, it needs a string.

However, the parser will read 233id as a number (233), then a symbol (id),
will convert the number to a string, but what to do with the symbod id?
This is the error message you got.

[some more deleted]

--
Kai
[email-masked]
An uncarable Linux hacker.

Advertisement by Microsoft in a well-known German magazine:
	If you don't like our programmes, than make your own ones.
However, they expect you to use Microsoft products for this -:)