Re: database access via c++ program...

Lists: pgsql-hackers
From: "Emilio Uy III" <emilio(at)qncos(dot)nec(dot)co(dot)jp>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: database access via c++ program...
Date: 2002-06-26 03:22:54
Message-ID: 015901c21cc0$c28c2c20$950c680a@qncos.nec.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi, my name is Emil, I am a software engineer at NEC, Japan. This is not in
any way related to my work, but I am that interested in cygwin that I
installed it on my own PC and right now I am enjoying it. Your work is a
good one really, I appreciate what you guys are doing.

I am just wondering, because I have installed PostgreSQL on my Cygwin
(Windows NT) and this is my first time to attempt a dive into the database
realm. I am not sure of this, but there should be a way to access a database
through say, a C++ program. Right now my Cygwin-PostgreSQL database is
working fine, I was able to create databases and tables, view them, modify
the contents, etc. But what I really want to do right now is to create a
simple program that would successfully connect to my PostgreSQL databases (i
have to specify the database i assume). Just a 5-liner maybe, just to test
the connection.

I tried and looked for some sample codes in the internet but I cant quite
find a good one. I hope you guys can help me.

My PostgreSQL is running okay, i just dont know what functions should be
called, what header files to include, etc.

Thank you for your time guys, and i hope to hear from you soon.

PS
Maybe in the future, when I get to know a lot about Cygwin, I would
contact you guys and join your support group probably... :-)

Sincerely,
Emilio Uy III

@@@@@@@@@@@@@@@@@@@
          Emilio Uy III
        エミリオ ウイ III    

     NEC通信システム九州(株)
 <メイル>  emilio(at)qncos(dot)nec(dot)co(dot)jp 
          e(dot)uy(at)pgs(dot)com(dot)ph
          emil_uy_iii(at)docomo(dot)ne(dot)jp
 <携帯電話> 090-24510266 
 <家電話>   092-8839411
@@@@@@@@@@@@@@@@@@@


From: jtv <jtv(at)xs4all(dot)nl>
To: Emilio Uy III <emilio(at)qncos(dot)nec(dot)co(dot)jp>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: database access via c++ program...
Date: 2002-06-26 14:42:26
Message-ID: 20020626144226.GD18491@xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jun 26, 2002 at 12:22:54PM +0900, Emilio Uy III wrote:
>
> I am just wondering, because I have installed PostgreSQL on my Cygwin
> (Windows NT) and this is my first time to attempt a dive into the database
> realm. I am not sure of this, but there should be a way to access a database
> through say, a C++ program. Right now my Cygwin-PostgreSQL database is
> working fine, I was able to create databases and tables, view them, modify
> the contents, etc. But what I really want to do right now is to create a
> simple program that would successfully connect to my PostgreSQL databases (i
> have to specify the database i assume). Just a 5-liner maybe, just to test
> the connection.

If you're using gcc or a similarly good compiler (Visual C++ will NOT work),
try libpqxx:

http://members.ams.chello.nl/j.vermeulen31/proj-libpqxx.html

I don't know how well it installs under Cygwin, but a simple test program
using libpqxx as its C++ interface to PostgreSQL can be something like:

#include <iostream>

#include <pqxx/connection.h>
#include <pqxx/transaction.h>
#include <pqxx/result.h>

using namespace std;
using namespace pqxx;

int main()
{
try
{
Connection C("dbname=mydatabase");
Transaction T(C, "T");
Result R = T.Exec("SELECT * FROM pg_tables");

for (Result::const_iterator c = R.begin();
c != R.end();
++c)
{
cout << c[0].c_str() << endl;
}

T.Commit();
}
catch (const exception &e)
{
cerr << e.what() << endl;
return 1;
}

return 0;
}

HTH,

Jeroen