Thread: libpq

libpq

From
Jim Michaels
Date:
I am trying to compile a program with libpq using boirland c++ 5.5.1 free edition and with mingw(gcc), and SELECT queries are returning PQntuples with results of 0, but I can do the same query in pgadmin and get 1 result.



there is no way to link on the .lib files because they are in COFF format.  what compiler did they use to make these libs?  could somebody please make libs and separate DLLs (that is important - they are different inside) available for the mingw compiler?  It is a free compiler available at mingw.org.

the borland c++ 5.5.1 (free)-compiled program does absolutely nothing, I think because the DLL's are not compatible.

                    sprintf(querystr,
                    "SELECT * FROM s_phonelist.phonelist\n"
                    "WHERE %s\n"
                    "ORDER BY lastname,firstname,middlename,country,stateprovince,city,mailstop,company"
                    , s
                    );
#if defined(_DEBUG)
                    printf("QUERY:\"%s\"\n", querystr);
#endif
                    pgr = (PQexec)(pgc,querystr);


                    if (0 == ntuples) {
                        printf("--no records.--\n");
                        (PQfinish)(pgc);
                        return 0;
                    } else {

                        for (index=0; index < ntuples; index++) {
                            //copy values from row to Crecord row
                            val = (PQgetvalue)(pgr,index, 0);strcpy(row.firstname    , val);
                            val = (PQgetvalue)(pgr,index, 1);strcpy(row.middlename   , val);
                            val = (PQgetvalue)(pgr,index, 2);strcpy(row.lastname     , val);
                            val = (PQgetvalue)(pgr,index, 3);strcpy(row.homephone    , val);
                            val = (PQgetvalue)(pgr,index, 4);strcpy(row.workphone    , val);
                            val = (PQgetvalue)(pgr,index, 5);strcpy(row.cellphone    , val);
                            val = (PQgetvalue)(pgr,index, 6);strcpy(row.pager        , val);
                            val = (PQgetvalue)(pgr,index, 7);strcpy(row.company      , val);
                            val = (PQgetvalue)(pgr,index, 8);strcpy(row.address1     , val);
                            val = (PQgetvalue)(pgr,index, 9);strcpy(row.address2     , val);
                            val = (PQgetvalue)(pgr,index,10);strcpy(row.city         , val);
                            val = (PQgetvalue)(pgr,index,11);strcpy(row.mailstop     , val);
                            val = (PQgetvalue)(pgr,index,12);strcpy(row.stateprovince, val);
                            val = (PQgetvalue)(pgr,index,13);strcpy(row.postalcode   , val);
                            val = (PQgetvalue)(pgr,index,14);strcpy(row.country      , val);
                            val = (PQgetvalue)(pgr,index,15);strcpy(row.comment      , val);
                            val = (PQgetvalue)(pgr,index,16);strcpy(row.website      , val);
                            val = (PQgetvalue)(pgr,index,17);strcpy(row.emailhome    , val);
                            val = (PQgetvalue)(pgr,index,18);strcpy(row.emailwork    , val);
                            //now display in an orderly fashion.
                            showrecord(row);
                        }
                    }






while (stone != rolling) moss++;
---
Computer memory/disk size measurements:
[KB KiB] [MB MiB] [GB GiB] [TB TiB]
[10^3B=1000B=1KB][10^6B=1000000B=1MB][10^9B=1000000000B=1GB][10^12B=1000000000000B=1TB]
[2^10B=1024B=1KiB][2^20B=1048576B=1MiB][2^30B=1073741824B=1GiB][2^40B=1099511627776B=1TiB]
Note that with disks, a disk size is measured in GB or TB, not in GiB or TiB.  computer memory (RAM) is measured in MiB and GiB.
---
new cyber dog food: Cables n' Bits
---




Re: libpq

From
Scott Ribe
Date:
That's pretty confused C code. The most obvious problem is that you're not
calling the Pqntuples function; you're just examining the value of a
variable called ntuples, when you haven't set that value after calling
Pqexec (and maybe have never set it).

Take it step by step, and check error returns at each step--including
connecting to the database, and of course especially check errors after
calling PQexec--first checking that pgr is not null, then if not null using
the PQresultStatus, PQresStatus, PQresultErroMessage functions, otherwise
the PQstatus, PQerrorMessage functions.

Then if you still have problems, post more complete code that includes
important things like connecting to the database, and declarations &
assignments to key variables.

Also, what is this "(function)(args)" stuff? Normally, the PQ functions are
plain C functions, called as "function(args)". Do you really have some setup
where you have function pointer variables and your compiler requires that
outdated syntax? Or is this more basic C confusion?

--
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice



Re: libpq

From
Jim Michaels
Date:
these are straight dll calls as outlined in Using Run-Time Dynamic Linking (Windows)
that's why they look funny.  it is impossible to link VC++ .lib files with mingw(gcc) .a libraries.

yeah, I just found the PQntuples bug myself too, and got the program finished.  thank you for the tips on using libpq, I may still need to implement those. 

I didn't remember seeing anywhere in the docs that you were supposed to check for pqr==NULL, I wish they would document that in PQexec. must be a documentation bug. There is no mention of return values! http://www.postgresql.org/docs/8.4/interactive/libpq-exec.html

still doesn't solve the need for MingW *.a  libraries version of libpq.  I wouldn't have had to rewrite the whole thing for this to work with mingw.  The DLL's are VC++/MinGW compatible, but the .lib files are not.





From: Scott Ribe <scott_ribe@killerbytes.com>
To: Jim Michaels <jmichae3@yahoo.com>; pgsql general <pgsql-general@postgresql.org>
Sent: Sunday, August 9, 2009 9:16:34 AM
Subject: Re: [GENERAL] libpq

That's pretty confused C code. The most obvious problem is that you're not
calling the Pqntuples function; you're just examining the value of a
variable called ntuples, when you haven't set that value after calling
Pqexec (and maybe have never set it).

Take it step by step, and check error returns at each step--including
connecting to the database, and of course especially check errors after
calling PQexec--first checking that pgr is not null, then if not null using
the PQresultStatus, PQresStatus, PQresultErroMessage functions, otherwise
the PQstatus, PQerrorMessage functions.

Then if you still have problems, post more complete code that includes
important things like connecting to the database, and declarations &
assignments to key variables.

Also, what is this "(function)(args)" stuff? Normally, the PQ functions are
plain C functions, called as "function(args)". Do you really have some setup
where you have function pointer variables and your compiler requires that
outdated syntax? Or is this more basic C confusion?

--
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice



Re: libpq

From
Merlin Moncure
Date:
On Mon, Aug 10, 2009 at 4:03 PM, Jim Michaels<jmichae3@yahoo.com> wrote:
> these are straight dll calls as outlined in Using Run-Time Dynamic Linking
> (Windows)
> that's why they look funny.  it is impossible to link VC++ .lib files with
> mingw(gcc) .a libraries.

that is not why they look funny...they look funny because you made them so :-).

function pointers mapped at runtime via dynamic linking do  not need
to be wrapped with ().

val = (PQgetvalue)(pgr,index, 0);strcpy(row.firstname    , val);

could be re-written as:
strcpy(row.firstname, PQgetvalue(pgr,index, 0));

...which is still asking for trouble...you're not checking the length
and blindly copying your results into the receiving structure.

Also, you are mixing 'SELECT *' with assumed column positions.  This
is IMO very bad style. either use explicit column list in your select
statement or pull your data from the result using name instead of
position.

Also, use a compiler which tells you about uninitialized variables,
don't top post, and avoid html emails when posting to public lists
:-).

merlin

Re: libpq

From
Scott Ribe
Date:
> I didn't remember seeing anywhere in the docs that you were supposed to check
> for pqr==NULL, I wish they would document that in PQexec.

It's right there in the first sentence of the discussion.

> There is no mention of return values!

???

--
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice