Hi,
On 6/3/05, David Hinkle <drachs@gmail.com> wrote:
> [snipped]
> What I'm asking for is an expample or a document showing me how to do
> it.
While trying to prepare an example for your question, I scracthed sth.
like this:
{{{ Code snippet
const char *command = "INSERT INTO tbl1 VALUES ($1)";
int nParams = 1;
const Oid paramTypes[] = {17}; // 17, for bytea type's OID.
const char * const paramValues[] = {"\\101\\102\\103"}; // "ABC"
const int *paramLengths = NULL; // Letting the backend to
const int *paramFormats = NULL; // deduce these params.
int resultFormat = 0; // Result will be in text format.
conn = PQconnectdb("dbname=test");
if ( PQstatus(conn) != CONNECTION_OK ) { fprintf(stderr, "Connection failed!\n%s", PQerrorMessage(conn));
exit_nicely(conn);
}
printf("Command: \"%s\",\n", command);
printf("Param. : \"%s\".\n", paramValues[0]);
res = PQexecParams(conn, command, nParams, paramTypes, paramValues, paramLengths, paramFormats,
resultFormat);
if ( PQresultStatus(res) != PGRES_COMMAND_OK ) { fprintf(stderr, "Insert failed!\n%s", PQresultErrorMessage(res));
PQclear(res); exit_nicely(conn);
} else { printf("Insert succeeded.\n"); PQclear(res);
}
}}}
Above code is working well for me. But while trying some other
combinations (out of NULL usage) for PQexecParams parameters, I
realized that when we use "const int paramFormats[] = {1};" execution
dumps SegFault:
{{{ Command output snippet
$ gcc -g -Wall -lpq binIns.c && ./a.out
Command: "INSERT INTO tbl1 VALUES ($1)",
Param. : "\101\102\103".
Segmentation fault
$ gdb ./a.out
[snipped]
Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run
Starting program: /home/knt/temp/a.out
Command: "INSERT INTO tbl1 VALUES ($1)",
Param. : "\101\102\103".
Program received signal SIGSEGV, Segmentation fault.
0xb7fcee05 in PQsendQueryGuts () from /usr/lib/libpq.so.4
(gdb) bt
#0 0xb7fcee05 in PQsendQueryGuts () from /usr/lib/libpq.so.4
#1 0xb7fce902 in PQsendQueryParams () from /usr/lib/libpq.so.4
#2 0xb7fcf420 in PQexecParams () from /usr/lib/libpq.so.4
#3 0x08048811 in main () at binIns.c:34
}}}
[Line 34: "res = PQexecParams(conn, ..."]
Is this an expected behaviour or did I miss sth. important?
Regards.