logo       

Re: libpq, PQExecParams and the inserting of binary data: msg#00017

Subject: Re: libpq, PQExecParams and the inserting of binary data
Hi,

On 6/3/05, David Hinkle <drachs@xxxxxxxxx> 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.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
      joining column's datatypes do not match



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe