Thread: Return PGresult
Hi, PGresult is a structure. I want to return PGresult pointer.(Code is given below.) Is this possible ? if yes , please give me the solution. Thanks in Advance. Anuj =========================== /* tempdb=>create table tbltemp (id int4, name varchar(30)); */ #include <stdio.h> #include <pgsql/libpq-fe.h> main() { int totalrec; PGresult *resu; dbQuery("tempdb","select * from tbltemp",&totalrec,resu); printf("\n The total rec = %i\n", totalrec); printf(" The total rec = %i\n", PQntuples(resu)); } /* End of main */ dbQuery(char * dbName, char *sqlstatement , int *totalrec,PGresult *res) { PGconn *conn; char fullsql[500]; conn = PQsetdb(NULL, NULL, NULL, NULL, dbName); res = PQexec(conn, "BEGIN"); PQclear(res); strcpy(fullsql , "DECLARE mycursor CURSOR FOR "); strcat(fullsql,sqlstatement); res = PQexec(conn, fullsql); PQclear(res); res = PQexec(conn, "FETCH ALL in mycursor"); *totalrec = PQntuples(res); } =========================== Out put : $ The total rec = 2 // This result is right. The total rec = 0 // The same result is not showing. $
anuj wrote: > > Hi, > PGresult is a structure. > I want to return PGresult pointer.(Code is given below.) > Is this possible ? if yes , please give me the solution. > Thanks in Advance. > Anuj > =========================== > /* tempdb=>create table tbltemp (id int4, name varchar(30)); */ > #include <stdio.h> > #include <pgsql/libpq-fe.h> > > main() > { > int totalrec; > PGresult *resu; > dbQuery("tempdb","select * from tbltemp",&totalrec,resu); > printf("\n The total rec = %i\n", totalrec); > printf(" The total rec = %i\n", PQntuples(resu)); > } /* End of main */ > > dbQuery(char * dbName, char *sqlstatement , int *totalrec,PGresult *res) > { > PGconn *conn; > char fullsql[500]; > conn = PQsetdb(NULL, NULL, NULL, NULL, dbName); > res = PQexec(conn, "BEGIN"); > PQclear(res); > strcpy(fullsql , "DECLARE mycursor CURSOR FOR "); > strcat(fullsql,sqlstatement); > res = PQexec(conn, fullsql); > PQclear(res); > res = PQexec(conn, "FETCH ALL in mycursor"); > *totalrec = PQntuples(res); > } > =========================== > > Out put : > $ > The total rec = 2 // This result is right. > The total rec = 0 // The same result is not showing. > $ Try dbQuery("tempdb","select * from tbltemp",&totalrec,&resu); and the function declaration dbQuery(char *dbName,char *sqlststatement,int *totalrec,PGresult **res); and then within the function reference it as *res= -- Dave Smith Candata Systems Ltd. (416) 493-9020 dave@candata.com
Thanks,I'm trying PGresult pointer to pointer. And Why don't PGresult pointer variable reqiure memory allocation (malloc()? Like /* resu = (PGresult *)(malloc(sizeof(PGresult)); */ -----Original Message----- From: dave@playpen.candata.com [mailto:dave@playpen.candata.com]On Behalf Of Dave Smith Sent: Thursday, July 20, 2000 5:21 PM To: anuj Cc: pgsql-general@postgresql.org Subject: Re: [GENERAL] Return PGresult anuj wrote: > > Hi, > PGresult is a structure. > I want to return PGresult pointer.(Code is given below.) > Is this possible ? if yes , please give me the solution. > Thanks in Advance. > Anuj > =========================== > /* tempdb=>create table tbltemp (id int4, name varchar(30)); */ > #include <stdio.h> > #include <pgsql/libpq-fe.h> > > main() > { > int totalrec; > PGresult *resu; > dbQuery("tempdb","select * from tbltemp",&totalrec,resu); > printf("\n The total rec = %i\n", totalrec); > printf(" The total rec = %i\n", PQntuples(resu)); > } /* End of main */ > > dbQuery(char * dbName, char *sqlstatement , int *totalrec,PGresult *res) > { > PGconn *conn; > char fullsql[500]; > conn = PQsetdb(NULL, NULL, NULL, NULL, dbName); > res = PQexec(conn, "BEGIN"); > PQclear(res); > strcpy(fullsql , "DECLARE mycursor CURSOR FOR "); > strcat(fullsql,sqlstatement); > res = PQexec(conn, fullsql); > PQclear(res); > res = PQexec(conn, "FETCH ALL in mycursor"); > *totalrec = PQntuples(res); > } > =========================== > > Out put : > $ > The total rec = 2 // This result is right. > The total rec = 0 // The same result is not showing. > $ Try dbQuery("tempdb","select * from tbltemp",&totalrec,&resu); and the function declaration dbQuery(char *dbName,char *sqlststatement,int *totalrec,PGresult **res); and then within the function reference it as *res= -- Dave Smith Candata Systems Ltd. (416) 493-9020 dave@candata.com
anuj wrote: > > Thanks,I'm trying PGresult pointer to pointer. > And Why don't PGresult pointer variable reqiure memory allocation (malloc()? > Like /* resu = (PGresult *)(malloc(sizeof(PGresult)); */ > I believe that the PQexec is doing the allocation. That is why it has to be pointer to pointer. -- Dave Smith Candata Systems Ltd. (416) 493-9020 dave@candata.com
Thanks, Now, I am able to return PGresult. Pointer to pointer is working fine. And about memory allocation. Before the PQexec we can't decide required memory. So, PQexec doing the memory allocation. Cherries Anuj >Try >dbQuery("tempdb","select * from tbltemp",&totalrec,&resu); >and the function declaration >dbQuery(char *dbName,char *sqlststatement,int *totalrec,PGresult **res); >and then within the function reference it as *res= -----Original Message----- From: pgsql-general-owner@hub.org [mailto:pgsql-general-owner@hub.org]On Behalf Of Dave Smith Sent: Thursday, July 20, 2000 5:46 PM To: anuj Cc: pgsql-general@postgresql.org Subject: Re: [GENERAL] Return PGresult anuj wrote: > > Thanks,I'm trying PGresult pointer to pointer. > And Why don't PGresult pointer variable reqiure memory allocation (malloc()? > Like /* resu = (PGresult *)(malloc(sizeof(PGresult)); */ > I believe that the PQexec is doing the allocation. That is why it has to be pointer to pointer. -- Dave Smith Candata Systems Ltd. (416) 493-9020 dave@candata.com