pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <volkan.yazici@gmail.com>
To: David Hinkle <drachs@gmail.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: libpq, PQExecParams and the inserting of binary data
Date: Sat, 4 Jun 2005 11:22:38 +0300
Message-ID: <7104a73705060401222c6cc5d2@mail.gmail.com> (raw)
In-Reply-To: <ae830e6c05060310367c37a84a@mail.gmail.com>
References: <ae830e6c05060309182201f85b@mail.gmail.com>
	<7104a7370506030943662e4be@mail.gmail.com>
	<ae830e6c05060310367c37a84a@mail.gmail.com>

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.



view thread (18+ messages)  latest in thread

Message-ID: <7104a73705060401222c6cc5d2@mail.gmail.com>
Permalink:  ../7104a73705060401222c6cc5d2@mail.gmail.com/
Also on:    postgresql.org/message-id/7104a73705060401222c6cc5d2@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: volkan.yazici@gmail.com, drachs@gmail.com
  Subject: Re: libpq, PQExecParams and the inserting of binary data
  In-Reply-To: <7104a73705060401222c6cc5d2@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox