Skip site navigation (1) Skip section navigation (2)

Re: libpq, PQExecParams and the inserting of binary data

From: Volkan YAZICI <volkan(dot)yazici(at)gmail(dot)com>
To: David Hinkle <drachs(at)gmail(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq, PQExecParams and the inserting of binary data
Date: 2005-06-04 08:22:38
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-interfaces

On 6/3/05, David Hinkle <drachs(at)gmail(dot)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));

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));
} else {
    printf("Insert succeeded.\n");

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 
Using host libthread_db library "/lib/tls/".

(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/
(gdb) bt
#0  0xb7fcee05 in PQsendQueryGuts () from /usr/lib/
#1  0xb7fce902 in PQsendQueryParams () from /usr/lib/
#2  0xb7fcf420 in PQexecParams () from /usr/lib/
#3  0x08048811 in main () at binIns.c:34

[Line 34: "res = PQexecParams(conn, ..."]

Is this an expected behaviour or did I miss sth. important?


In response to


pgsql-interfaces by date

Next:From: Daniel VeriteDate: 2005-06-04 13:36:20
Subject: Re: libpq, PQExecParams and the inserting of binary data
Previous:From: Daniel VeriteDate: 2005-06-03 19:29:14
Subject: Re: libpq, PQExecParams and the inserting of binary data

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group