Re: [INTERFACES] question on accessing PostgreSQL with C

From: James Thompson <jamest(at)math(dot)ksu(dot)edu>
To: hoelc <hoelc(at)pd(dot)jaring(dot)my>
Cc: pgsql-interfaces(at)postgreSQL(dot)org
Subject: Re: [INTERFACES] question on accessing PostgreSQL with C
Date: 1999-02-07 01:16:14
Message-ID: Pine.GSO.4.05.9902061850530.6735-100000@noether.math.ksu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

On Sun, 7 Feb 1999, hoelc wrote:

>
> (2) How to put variables into the SQL?
> I use C program to access PostgreSQL with libpq, and use PQexec(
> ) to send the query. One of the failed example of mine,
>

I'd recommend using ecpg that comes with postgresql in the place of libpq
direct. It is IMHO orders of magnitude easier than libpg alone. The
syntax follows the same as Oracle's Pro*C so you can pretty much use any
of their manuals and get a working program. I found an on-line copy of
the Pro*c manual at

http://www.dal.ca/%7eoracle/oradoc/DOC/api/doc/PC_22/toc.htm

With it you end up with something like the following in your code

exec sql
insert into customer(id,name_first,name_middle,name_last,company,
address1,address2,city,state,country,zip,phone_work,
phone_home,phone_fax,email,date_entered,billing_terms,
confirmation_method)
values (:id,:name_first, :name_middle, :name_last,
:company,:address1,:address2,
:city,:state,:country,:zip,:phone_work,
:phone_home,:phone_fax,:email,date('now'),
:billing_terms,
:confirmation_method);

the names preceeded by : are c variables. The ecpg preprocessor replaces
this easily readable code with much nastier looking c code that does what
you require.

The author of the code is very responsive to few problem reports I have
sent him and has sent patches, or workarounds to me usually within 24
hours.

In the few cases you can't figure out a way to make something happen using
ecpg I believe you can mix the libpg and ecpg code in the same program.
The only place I could see a need for this is when you'd need the
functionality given by Pro*C's Dynamic SQL which ecpg doesn't yet support,
but I think its in the works.

You can find examples of ecpg code in the source tree
postgresql-6.4.2/src/interfaces/ecpg/test

>
> (3) How to check who are the users in PostgreSQL system?
>

select * from pg_user;

->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<
James Thompson 138 Cardwell Hall Manhattan, Ks 66506 785-532-0561
Kansas State University Department of Mathematics
->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<

In response to

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Dinesh Patil 1999-02-07 01:42:29 help on libpq
Previous Message hoelc 1999-02-07 00:45:22 question on accessing PostgreSQL with C