hi,
the following code give the following error beneath and no reason for 
that error in my mind the error is yielded by running the script twice 
it accept only to drop the table once after though the code refuse to 
create it again and hence refuse to fill it with data can you help in that

here is the code:
******************************************************************************************************************************************************
psql_test.c
******************************************************************************************************************************************************
#include <string.h>
#include <libpq-fe.h>

 
/* close connection to database */
void close_conn(PGconn *conn)
{
    PQfinish(conn);
}
 
/* establish connection to database */
PGconn *connect_db()
{
    PGconn *conn = NULL;
 
    // Make a connection to the database
    conn = PQconnectdb("user='postgres' password='123' dbname='contact' 
hostaddr=127.0.0.1 port=5432");
 
    // Check to see that the backend connection was successfully made 
    if (PQstatus(conn) != CONNECTION_OK)
    {
        printf("connection to database failed");
        close_conn(conn);
    }
    else
    {
    printf("connection to database - ok\n");
    }
    return (conn);
}
 
/* create employee table */
void create_employee_table(PGconn *conn)
{
    // execute with sql statement
    PGresult *res = PQexec(conn, "create  table employee (fname 
char(30), lname char(30))");
    
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("create employee table failed");
        PQclear(res);
        close_conn(conn);
    }
    else
    {
    printf("create employee table - ok\n");
    }
 
    // clear result
    PQclear(res);
}
 
/* append sql statement and insert record into employee table */
void insert_employee_rec(PGconn *conn, char* fname, char* lname)
{
    // Append the SQL statment
    char*  sSQL;
    sprintf(sSQL, "insert into employee  values ('%s', '%s');",  fname , 
lname );
     
    // Execute with sql statement
    PGresult *res = PQexec(conn, sSQL);
 
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("insert employee record failed");
        PQclear(res);
        close_conn(conn);
    }
    else
    {
    printf("insert employee record - OK\n");
    }
    //create_employee_table clear result
    PQclear(res);
}
 
/* fetch employee record and display it on screen */
void fetch_employee_rec(PGconn *conn)
{
    // will hold the number of field in employee table
    int nFields;
 
    // start a transaction block
    PGresult *res  = PQexec(conn, "begin");
 
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("begin command failed");
        PQclear(res);
        close_conn(conn);
    }
 
   // clear result
    PQclear(res);
 
    // fetch rows from employee table
    res = PQexec(conn, "declare emprec cursor for select * from employee");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("declare cursor failed");
        PQclear(res);
        close_conn(conn);
    }
 
    // clear result
    PQclear(res);
 
    res = PQexec(conn, "fetch all in emprec");
 
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        printf("fetch all failed");
        PQclear(res);
        close_conn(conn);
    }
 
    // get the field name
    nFields = PQnfields(res);
 
    // prepare the header with employee table field name
    printf("\nfetch employee record:");
    
printf("\n********************************************************************\n");
 

    for (int i = 0; i < nFields; i++)
        printf("%-30s", PQfname(res, i));
    
printf("\n********************************************************************\n");
 

 
    // next, print out the employee record for each row
    for (int i = 0; i < PQntuples(res); i++)
    {
        for (int j = 0; j < nFields; j++)
            printf("%-30s", PQgetvalue(res, i, j));
        printf("\n");
    }
     
    PQclear(res);
 
    // close the emprec
    res = PQexec(conn, "close emprec");
    PQclear(res);
 
    // end the transaction
    res = PQexec(conn, "end");
 
    // clear result
    PQclear(res);
}
 
/* erase all record in employee table */
void remove_all_employee_rec(PGconn *conn)
{
    // execute with sql statement
    PGresult *res = PQexec(conn, "delete from employee");
 
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("delete employees record failed.");
        PQclear(res);
        close_conn(conn);
    }
    else
    {
    printf("\ndelete employees record - ok\n");
    }
    // clear result
    PQclear(res);
}
 
/* drop employee table from the database*/
void drop_employee_table(PGconn *conn)
{
    // execute with sql statement
    PGresult *res = PQexec(conn, "drop table employee");
 
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        printf("drop employee table failed.");
        PQclear(res);
        close_conn(conn);
    }
    else
    {
    printf("drop employee table - ok\n");
    }
    // clear result
    PQclear(res);
}
 
int main()
{
    PGconn     *conn = NULL;
    conn = connect_db();
    create_employee_table(conn);
     
    insert_employee_rec(conn, "nour", "seef");
    insert_employee_rec(conn, "noriah", "sifh");
    fetch_employee_rec(conn);
 
    printf("\npress enter to remove all records & table.....\n");
    getchar();
 
    remove_all_employee_rec(conn);
    drop_employee_table(conn);
     
    close_conn(conn);
 
    return (0);
}
 

=======================================================================================================================================
the above code give the following error
=======================================================================================================================================
[root@localhost 006]# bash c006
connection to database - ok
Insert employee record failedInsert employee record - OK
*** glibc detected *** ../../object/006/psql_test.bin: double free or 
corruption (top): 0x085dfea8 ***
======= Backtrace: =========
/lib/libc.so.6[0x6366c5]
/lib/libc.so.6(cfree+0x59)[0x636b09]
/usr/lib/libpq.so.5(PQclear+0xf0)[0x794f50]
../../object/006/psql_test.bin[0x8048b6b]
../../object/006/psql_test.bin[0x8048c6f]
/lib/libc.so.6(__libc_start_main+0xdc)[0x5e2e9c]
../../object/006/psql_test.bin(__gxx_personality_v0+0x65)[0x8048711]
======= Memory map: ========
0049b000-004c1000 r-xp 00000000 fd:00 10977453   /usr/lib/libk5crypto.so.3.1
004c1000-004c2000 rwxp 00025000 fd:00 10977453   /usr/lib/libk5crypto.so.3.1
004c4000-004f1000 r-xp 00000000 fd:00 10977455   
/usr/lib/libgssapi_krb5.so.2.2
004f1000-004f2000 rwxp 0002d000 fd:00 10977455   
/usr/lib/libgssapi_krb5.so.2.2
004f4000-00538000 r-xp 00000000 fd:00 22317439   /lib/libssl.so.0.9.8e
00538000-0053c000 rwxp 00043000 fd:00 22317439   /lib/libssl.so.0.9.8e
005ae000-005c9000 r-xp 00000000 fd:00 22315172   /lib/ld-2.5.so
005c9000-005ca000 r-xp 0001a000 fd:00 22315172   /lib/ld-2.5.so
005ca000-005cb000 rwxp 0001b000 fd:00 22315172   /lib/ld-2.5.so
005cd000-00720000 r-xp 00000000 fd:00 22315232   /lib/libc-2.5.so
00720000-00722000 r-xp 00153000 fd:00 22315232   /lib/libc-2.5.so
00722000-00723000 rwxp 00155000 fd:00 22315232   /lib/libc-2.5.so
00723000-00726000 rwxp 00723000 00:00 0
00728000-0074f000 r-xp 00000000 fd:00 22315263   /lib/libm-2.5.so
0074f000-00750000 r-xp 00026000 fd:00 22315263   /lib/libm-2.5.so
00750000-00751000 rwxp 00027000 fd:00 22315263   /lib/libm-2.5.so
00753000-00756000 r-xp 00000000 fd:00 22315253   /lib/libdl-2.5.so
00756000-00757000 r-xp 00002000 fd:00 22315253   /lib/libdl-2.5.so
00757000-00758000 rwxp 00003000 fd:00 22315253   /lib/libdl-2.5.so
0075a000-0076f000 r-xp 00000000 fd:00 22315235   /lib/libpthread-2.5.so
0076f000-00770000 r-xp 00015000 fd:00 22315235   /lib/libpthread-2.5.so
00770000-00771000 rwxp 00016000 fd:00 22315235   /lib/libpthread-2.5.so
00771000-00773000 rwxp 00771000 00:00 0
00775000-00787000 r-xp 00000000 fd:00 27419696   /usr/lib/libz.so.1.2.3
00787000-00788000 rwxp 00011000 fd:00 27419696   /usr/lib/libz.so.1.2.3
0078a000-007ad000 r-xp 00000000 fd:00 27423657   /usr/lib/libpq.so.5.2
007ad000-007af000 rwxp 00022000 fd:00 27423657   /usr/lib/libpq.so.5.2
00aaf000-00ac5000 r-xp 00000000 fd:00 22315285   /lib/libselinux.so.1
00ac5000-00ac7000 rwxp 00015000 fd:00 22315285   /lib/libselinux.so.1
00acf000-00b0a000 r-xp 00000000 fd:00 22315274   /lib/libsepol.so.1
00b0a000-00b0b000 rwxp 0003b000 fd:00 22315274   /lib/libsepol.so.1
00b0b000-00b15000 rwxp 00b0b000 00:00 0
00cf4000-00cf5000 r-xp 00cf4000 00:00 0          [vdso]
00cff000-00d0f000 r-xp 00000000 fd:00 22317430   /lib/libresolv-2.5.so
00d0f000-00d10000 r-xp 0000f000 fd:00 22317430   /lib/libresolv-2.5.so
00d10000-00d11000 rwxp 00010000 fd:00 22317430   /lib/libresolv-2.5.so
00d11000-00d13000 rwxp 00d11000 00:00 0
00d15000-00d20000 r-xp 00000000 fd:00 22315273   
/lib/libgcc_s-4.1.2-20080825.so.1
00d20000-00d21000 rwxp 0000a000 fd:00 22315273   
/lib/libgcc_s-4.1.2-20080825.so.1
00d23000-00d25000 r-xp 00000000 fd:00 22317426   /lib/libkeyutils-1.2.so
00d25000-00d26000 rwxp 00001000 fd:00 22317426   /lib/libkeyutils-1.2.so
00d28000-00d2a000 r-xp 00000000 fd:00 22317434   /lib/libcom_err.so.2.1
00d2a000-00d2b000 rwxp 00001000 fd:00 22317434   /lib/libcom_err.so.2.1
00d51000-00de5000 r-xp 00000000 fd:00 10977454   /usr/lib/libkrb5.so.3.3
00de5000-00de8000 rwxp 00093000 fd:00 10977454   /usr/lib/libkrb5.so.3.3
00dea000-00df2000 r-xp 00000000 fd:00 10977452   
/usr/lib/libkrb5support.so.0.1
00df2000-00df3000 rwxp 00007000 fd:00 10977452   
/usr/lib/libkrb5support.so.0.1
06152000-0627c000 r-xp 00000000 fd:00 22315268   /lib/libcrypto.so.0.9.8e
0627c000-0628f000 rwxp 00129000 fd:00 22315268   /lib/libcrypto.so.0.9.8e
0628f000-06293000 rwxp 0628f000 00:00 0
06295000-06375000 r-xp 00000000 fd:00 27406453   /usr/lib/libstdc++.so.6.0.8
06375000-06379000 r-xp 000df000 fd:00 27406453   /usr/lib/libstdc++.so.6.0.8
06379000-0637a000 rwxp 000e3000 fd:00 27406453   /usr/lib/libstdc++.so.6.0.8
0637a000-06380000 rwxp 0637a000 00:00 0
064df000-064e8000 r-xp 00000000 fd:00 22317445   /lib/libcrypt-2.5.so
064e8000-064e9000 r-xp 00008000 fd:00 22317445   /lib/libcrypt-2.5.so
064e9000-064ea000 rwxp 00009000 fd:00 22317445   /lib/libcrypt-2.5.so
064ea000-06511000 rwxp 064ea000 00:00 0
06941000-0694e000 r-xp 00000000 fd:00 27404768   
/usr/lib/liblber-2.3.so.0.2.31
0694e000-0694f000 rwxp 0000c000 fd:00 27404768   
/usr/lib/liblber-2.3.so.0.2.31
06b60000-06b78000 r-xp 00000000 fd:00 10977475   /usr/lib/libsasl2.so.2.0.22
06b78000-06b79000 rwxp 00017000 fd:00 10977475   /usr/lib/libsasl2.so.2.0.22
06cb1000-06cef000 r-xp 00000000 fd:00 10977476   
/usr/lib/libldap_r-2.3.so.0.2.31
06cef000-06cf1000 rwxp 0003d000 fd:00 10977476   
/usr/lib/libldap_r-2.3.so.0.2.31
06cf1000-06cf6000 rwxp 06cf1000 00:00 0
08048000-0804a000 r-xp 00000000 fd:00 30181479   
/data/hatem/work/c/project/learning/object/006/psql_test.bin
0804a000-0804b000 rw-p 00001000 fd:00 30181479   
/data/hatem/work/c/project/learning/object/006/psql_test.bin
085d7000-085f8000 rw-p 085d7000 00:00 0          [heap]
b7f59000-b7f60000 rw-p b7f59000 00:00 0
b7f74000-b7f75000 rw-p b7f74000 00:00 0
bfcbd000-bfcd2000 rw-p bffea000 00:00 0          [stack]
c006: line 22: 21745 Aborted                 ../../object/006/psql_test.bin
[root@localhost 006]#

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to