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

From Madhurima Das
Subject unable to insert column using postgresql and C
Date
Msg-id CAMsahj0bVxHCdeHjo-mo-SXTXO+PjM=d27O-GYRZ6ew3vVB-=A@mail.gmail.com
Whole thread Raw
Responses Re: unable to insert column using postgresql and C  (Fabrízio de Royes Mello <fabriziomello@gmail.com>)
List pgsql-students
Dear Sir/Madam,

I am a newbie in Postgresql and using C program to access it.

The following program (below; also in the attachment) reads an existing table "people" having four columns (id, lastname, firstname, phonenumber) and adds a new row (5, XXX, YYY, 7633839276) and a new column (TRUE).

After the proper building and linking and compiling, the program is successfully inserting the row but its unable to insert the column and I get the following output:

$ ./test 
Successfully inserted value in Table..... 
We did not get any data!

Can you please tell me where I am going wrong..

Thanks,
Madhurima


Program:

#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,"select lastname,firstname,phonenumber from people order by id");
  res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100);");
  res = PQexec(conn, "EXEC('UPDATE people SET comment = ''TRUE'';');");

    if(PQresultStatus(res) != PGRES_TUPLES_OK) {
puts("We did not get any data!");
        exit(0);
    }
 
rec_count = PQntuples(res);
 
    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;
}
Attachment

pgsql-students by date:

Previous
From: Vladislav Sterzhanov
Date:
Subject: GSoC'14: KNN on SP-GiST
Next
From: Fabrízio de Royes Mello
Date:
Subject: Re: unable to insert column using postgresql and C