logo       

Re: database access via c++ program...: msg#00901

Subject: Re: database access via c++ program...
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




---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
web.pylons.gene...    hurd.l4/2002-10...    kernel.commits....    user-groups.lin...    yellowdog.gener...    java.drools.use...    security.openva...    package-managem...    linux.debian.us...    qnx.openqnx.dev...    genealogy.gramp...    file-systems.if...    voip.wengophone...    tex.context/200...    ietf.smime/2003...    audio.csound.de...    culture.region....    xfree86.devel/2...    mobile.kannel.u...    distributed.con...    education.engli...    org.user-groups...    bug-tracking.gn...    recreation.bicy...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe