public inbox for [email protected]  
help / color / mirror / Atom feed
How to insert default value with libpq?
3+ messages / 2 participants
[nested] [flat]

* How to insert default value with libpq?
@ 2020-04-27 13:02 [email protected]
  2020-04-28 14:25 ` Re: How to insert default value with libpq? Peter Eisentraut <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: [email protected] @ 2020-04-27 13:02 UTC (permalink / raw)
  To: [email protected]

Hello,

I would like to insert the default value for a field with libpq, but
inserting null or "default" doesn’t work.

I attached a simple example also available here
https://gitlab.com/snippets/1970590

Is it possible?


Attachments:

  [text/x-csrc] main.c (988B, ../../[email protected]/2-main.c)
  download | inline:
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"

static void exec(PGconn* conn, const char* param)
{
    const char* values[1] = {param};

    PGresult* res = PQexecParams(
        conn,
        "INSERT INTO test (id) VALUES ($1);",
        sizeof(values) / sizeof(*values),
        NULL, // types
        values,
        NULL, // lengths
        NULL, // formats
        1
    );

    printf("%s", PQresultErrorMessage(res));

    PQclear(res);
}

int main(int argc, char** argv)
{
    const char* conninfo;

    if (argc > 1) {
        conninfo = argv[1];
    } else {
        conninfo = "host=localhost";
    }

    PGconn* conn = PQconnectdb(conninfo);

    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
        exit(1);
    }

    PQexec(conn, "CREATE TEMPORARY TABLE test (id SERIAL PRIMARY KEY);");

    exec(conn, NULL);
    exec(conn, "default");

    PQfinish(conn);

    return 0;
}

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to insert default value with libpq?
  2020-04-27 13:02 How to insert default value with libpq? [email protected]
@ 2020-04-28 14:25 ` Peter Eisentraut <[email protected]>
  2020-04-29 10:59   ` Re: How to insert default value with libpq? [email protected]
  0 siblings, 1 reply; 3+ messages in thread

From: Peter Eisentraut @ 2020-04-28 14:25 UTC (permalink / raw)
  To: [email protected]; [email protected]

On 2020-04-27 15:02, [email protected] wrote:
> I would like to insert the default value for a field with libpq, but
> inserting null or "default" doesn’t work.

You cannot represent that using query parameters (PQexecParams).  Query 
parameters always mean, use this rather than the default value.

To do what you want either write DEFAULT or leave off the column 
altogether, for example

     INSERT INTO test (id) VALUES (DEFAULT)

but this needs to be pasted into the query string, not passed as a 
parameter.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to insert default value with libpq?
  2020-04-27 13:02 How to insert default value with libpq? [email protected]
  2020-04-28 14:25 ` Re: How to insert default value with libpq? Peter Eisentraut <[email protected]>
@ 2020-04-29 10:59   ` [email protected]
  0 siblings, 0 replies; 3+ messages in thread

From: [email protected] @ 2020-04-29 10:59 UTC (permalink / raw)
  To: [email protected]


Le 28/04/2020 à 16:25, Peter Eisentraut a écrit :
> but this needs to be pasted into the query string, not passed as a
> parameter.

Fine, thank you for your help!





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-04-29 10:59 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 13:02 How to insert default value with libpq? [email protected]
2020-04-28 14:25 ` Peter Eisentraut <[email protected]>
2020-04-29 10:59   ` [email protected]

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