public inbox for [email protected]  
help / color / mirror / Atom feed
From: [email protected]
To: [email protected]
Subject: How to insert default value with libpq?
Date: Mon, 27 Apr 2020 15:02:38 +0200
Message-ID: <[email protected]> (raw)

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;
}

view thread (3+ messages)  latest in thread

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: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: How to insert default value with libpq?
  In-Reply-To: <[email protected]>

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

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