Re: unable to insert column using postgresql and C - Mailing list pgsql-students

From Fabrízio de Royes Mello
Subject Re: unable to insert column using postgresql and C
Date
Msg-id CAFcNs+o+Jq9exyDsND0aO0iWMzRnFm2FRjPFv=1WXBOVbQi3qw@mail.gmail.com
Whole thread Raw
In response to unable to insert column using postgresql and C  (Madhurima Das <madhurima.das@gmail.com>)
List pgsql-students
Hello,

I didn't test it, but try:

#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <string.h>

int main()
{
    PGconn *conn;
    PGresult *res;
    int rec_count;
    int row;
    int col;

    conn = PQconnectdb("dbname=test host=localhost user=abc password=xyz");

    if(PQstatus(conn) == CONNECTION_BAD) {
        puts("We were unable to connect to the database");
        exit(0);
    }

    res = PQexec(conn,"INSERT INTO people VALUES (5, 'XXX', 'YYY', '7633839276');");
    if(PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "Insertion Failed: %s", PQerrorMessage(conn));
        PQclear(res);
    }
    else
        printf("Successfully inserted value in Table..... \n");

    res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3");
    res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100);");
    res = PQexec(conn, "UPDATE people SET comment = 'TRUE';");

    res = PQexec(conn,"select lastname,firstname,phonenumber from people order by id");
    rec_count = PQntuples(res);

    if(PQresultStatus(res) != PGRES_TUPLES_OK) {
        puts("We did not get any data!");
        exit(0);
    }

    printf("We received %d records.\n", rec_count);
    puts("==========================");

    for(row=0; row<rec_count; row++) {
        for(col=0; col<3; col++) {
            printf("%s\t", PQgetvalue(res, row, col));
        }
        puts("");
    }

    puts("==========================");

    PQclear(res);

    PQfinish(conn);

    return 0;
}



--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello

pgsql-students by date:

Previous
From: Madhurima Das
Date:
Subject: unable to insert column using postgresql and C
Next
From: Rohit Goyal
Date:
Subject: Running DBT2 on postgresql