Thread: Getting to learn libpqxx

Getting to learn libpqxx

From
Michiel Lange
Date:
Hello everybody again,

I went out to investigate and try if I could master libpqxx, for libpq++ 
works not at all. Seeing it is discontinued, and people are encouraged to 
use libpqxx, I was so bold as to try that ;-)

Only to discover that there is hardly any documentation on how to access 
stuff... now I, myself am more a C-programmer, and I know C++ is pretty 
much different... it is really hard for me to think in classes and stuff, 
to think object-oriented...

So, now you know I am an earthling :P

I tried this kind of approach, the idea was to start out simple... so don't 
laugh at the attempt ...

#include <pqxx/connection.h>

int main(int argc, char *argv[])
{Connection *db;char uname[250]; // usernamechar passwd[250]; // passwordchar host[250]; //hostnamechar passwd[250];
//passwordchardbase[250]; //database namechar connstring[1264];... some output and input to get values for all these
chararrays...
 
connstring = "host=" >> host >> "database=" >> dbase >> "username=" >> 
uname >> "password=" >> passwd;db = new Connection(connstring,true);
if(db->is_open()){    cout << "Connection succesful!" << endl;    cout << db->Options();}else{    cout << "Something
wentwrong... oops" << endl;}delete db;return 0;
 
}

I get my first errors at "Connection *db" declaration... there is no 
Connection defined...
Can anyone point out how to reference the classes libpqxx has?

Also I was curious if my connstring was correctly built, but that is 
another subject I think... One other very important thing would be: how can 
I find out the error PostgreSQL will throw at me when I did something ugly? 
At least I can find out the problem, when I make one then...

Michiel



Re: Getting to learn libpqxx

From
"Matt Fitzgerald"
Date:
Ok... some stupid questions:

1. I take it pqxx/connection.h is in the include path?
2. If so, what command are you using to compile the source?

Also, your connection string is not built correctly, 'connstring' is not a
class, thus it doesn't support any overloaded operators such as '<<' & '+'
etc.

Do some thing like this:

#include <sstream>

std::stringstream connstring;

connstring << "host=" << host << "database=" << dbase << "username=" <<
uname << "password=" << passwd;

// call connection constructor something like this:
Connection *db = new Connection(connstring.str().c_str());


Etc.. etc.. also, as 'bool Immediate' defaults to 'true' you don't have to
specify it.

Regards, -Matt




-----Original Message-----
From: pgsql-interfaces-owner@postgresql.org
[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of Michiel Lange
Sent: Thursday, 10 April 2003 8:31 AM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] Getting to learn libpqxx

Hello everybody again,

I went out to investigate and try if I could master libpqxx, for libpq++
works not at all. Seeing it is discontinued, and people are encouraged to
use libpqxx, I was so bold as to try that ;-)

Only to discover that there is hardly any documentation on how to access
stuff... now I, myself am more a C-programmer, and I know C++ is pretty
much different... it is really hard for me to think in classes and stuff,
to think object-oriented...

So, now you know I am an earthling :P

I tried this kind of approach, the idea was to start out simple... so don't
laugh at the attempt ...

#include <pqxx/connection.h>

int main(int argc, char *argv[])
{Connection *db;char uname[250]; // usernamechar passwd[250]; // passwordchar host[250]; //hostnamechar passwd[250];
//passwordchardbase[250]; //database namechar connstring[1264];... some output and input to get values for all these
chararrays... 
connstring = "host=" >> host >> "database=" >> dbase >> "username="
>>
uname >> "password=" >> passwd;db = new Connection(connstring,true);
if(db->is_open()){    cout << "Connection succesful!" << endl;    cout << db->Options();}else{    cout << "Something
wentwrong... oops" << endl;}delete db;return 0; 
}

I get my first errors at "Connection *db" declaration... there is no
Connection defined...
Can anyone point out how to reference the classes libpqxx has?

Also I was curious if my connstring was correctly built, but that is
another subject I think... One other very important thing would be: how can
I find out the error PostgreSQL will throw at me when I did something ugly?
At least I can find out the problem, when I make one then...

Michiel


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org



Re: Getting to learn libpqxx

From
"Matt Fitzgerald"
Date:
Ok... some stupid questions:

1. I take it pqxx/connection.h is in the include path?
2. If so, what command are you using to compile the source?

Also, your connection string is not built correctly, 'connstring' is not a
class, thus it doesn't support any overloaded operators such as '<<' & '+'
etc.

Do some thing like this:

#include <sstream>

std::stringstream connstring;

connstring << "host=" << host << "database=" << dbase << "username=" <<
uname << "password=" << passwd;

// call connection constructor something like this:
Connection *db = new Connection(connstring.str().c_str());


Etc.. etc.. also, as 'bool Immediate' defaults to 'true' you don't have to
specify it.

Regards, -Matt




-----Original Message-----
From: pgsql-interfaces-owner@postgresql.org
[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of Michiel Lange
Sent: Thursday, 10 April 2003 8:31 AM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] Getting to learn libpqxx

Hello everybody again,

I went out to investigate and try if I could master libpqxx, for libpq++
works not at all. Seeing it is discontinued, and people are encouraged to
use libpqxx, I was so bold as to try that ;-)

Only to discover that there is hardly any documentation on how to access
stuff... now I, myself am more a C-programmer, and I know C++ is pretty
much different... it is really hard for me to think in classes and stuff,
to think object-oriented...

So, now you know I am an earthling :P

I tried this kind of approach, the idea was to start out simple... so don't
laugh at the attempt ...

#include <pqxx/connection.h>

int main(int argc, char *argv[])
{Connection *db;char uname[250]; // usernamechar passwd[250]; // passwordchar host[250]; //hostnamechar passwd[250];
//passwordchardbase[250]; //database namechar connstring[1264];... some output and input to get values for all these
chararrays... 
connstring = "host=" >> host >> "database=" >> dbase >> "username="
>>
uname >> "password=" >> passwd;db = new Connection(connstring,true);
if(db->is_open()){    cout << "Connection succesful!" << endl;    cout << db->Options();}else{    cout << "Something
wentwrong... oops" << endl;}delete db;return 0; 
}

I get my first errors at "Connection *db" declaration... there is no
Connection defined...
Can anyone point out how to reference the classes libpqxx has?

Also I was curious if my connstring was correctly built, but that is
another subject I think... One other very important thing would be: how can
I find out the error PostgreSQL will throw at me when I did something ugly?
At least I can find out the problem, when I make one then...

Michiel


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org



Re: Getting to learn libpqxx

From
"Matt Fitzgerald"
Date:
Ok... some stupid questions:

1. I take it pqxx/connection.h is in the include path?
2. If so, what command are you using to compile the source?

Also, your connection string is not built correctly, 'connstring' is not a
class, thus it doesn't support any overloaded operators such as '<<' & '+'
etc.

Do some thing like this:

#include <sstream>

std::stringstream connstring;

connstring << "host=" << host << "database=" << dbase << "username=" <<
uname << "password=" << passwd;

// call connection constructor something like this:
Connection *db = new Connection(connstring.str().c_str());


Etc.. etc.. also, as 'bool Immediate' defaults to 'true' you don't have to
specify it.

Regards, -Matt




-----Original Message-----
From: pgsql-interfaces-owner@postgresql.org
[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of Michiel Lange
Sent: Thursday, 10 April 2003 8:31 AM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] Getting to learn libpqxx

Hello everybody again,

I went out to investigate and try if I could master libpqxx, for libpq++
works not at all. Seeing it is discontinued, and people are encouraged to
use libpqxx, I was so bold as to try that ;-)

Only to discover that there is hardly any documentation on how to access
stuff... now I, myself am more a C-programmer, and I know C++ is pretty
much different... it is really hard for me to think in classes and stuff,
to think object-oriented...

So, now you know I am an earthling :P

I tried this kind of approach, the idea was to start out simple... so don't
laugh at the attempt ...

#include <pqxx/connection.h>

int main(int argc, char *argv[])
{Connection *db;char uname[250]; // usernamechar passwd[250]; // passwordchar host[250]; //hostnamechar passwd[250];
//passwordchardbase[250]; //database namechar connstring[1264];... some output and input to get values for all these
chararrays... 
connstring = "host=" >> host >> "database=" >> dbase >> "username="
>>
uname >> "password=" >> passwd;db = new Connection(connstring,true);
if(db->is_open()){    cout << "Connection succesful!" << endl;    cout << db->Options();}else{    cout << "Something
wentwrong... oops" << endl;}delete db;return 0; 
}

I get my first errors at "Connection *db" declaration... there is no
Connection defined...
Can anyone point out how to reference the classes libpqxx has?

Also I was curious if my connstring was correctly built, but that is
another subject I think... One other very important thing would be: how can
I find out the error PostgreSQL will throw at me when I did something ugly?
At least I can find out the problem, when I make one then...

Michiel


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org



Re: Getting to learn libpqxx

From
Michiel Lange
Date:
Ok... here are some answers:

Yes, pqxx/connection.h is in the include path, the errormessage I would get 
would be very different...

I compile the source with gcc: gcc source.cpp -o source
the libraries should be in place, I placed them in /usr/local/lib (and ran 
ldconfig)

I will try the #include <sstream.h> (I believe you forgot the .h), but from 
what I remember the
char foo[234];

foo = bar << "blah" << bla;

would be perfectly valid... but I can be mistaken... I will try your 
suggestion and see if it will help...
yet, the problem that the declaration (Connstring *db;) is not valid...

Michiel
At 17:32 10-4-2003 +1000, Matt Fitzgerald wrote:
>Ok... some stupid questions:
>
>1. I take it pqxx/connection.h is in the include path?
>2. If so, what command are you using to compile the source?
>
>Also, your connection string is not built correctly, 'connstring' is not a
>class, thus it doesn't support any overloaded operators such as '<<' & '+'
>etc.
>
>Do some thing like this:
>
>#include <sstream>
>
>std::stringstream connstring;
>
>connstring << "host=" << host << "database=" << dbase << "username=" <<
>uname << "password=" << passwd;
>
>// call connection constructor something like this:
>Connection *db = new Connection(connstring.str().c_str());
>
>
>Etc.. etc.. also, as 'bool Immediate' defaults to 'true' you don't have to
>specify it.
>
>Regards,
>   -Matt
>
>
>
>
>-----Original Message-----
>From: pgsql-interfaces-owner@postgresql.org
>[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of Michiel Lange
>Sent: Thursday, 10 April 2003 8:31 AM
>To: pgsql-interfaces@postgresql.org
>Subject: [INTERFACES] Getting to learn libpqxx
>
>Hello everybody again,
>
>I went out to investigate and try if I could master libpqxx, for libpq++
>works not at all. Seeing it is discontinued, and people are encouraged to
>use libpqxx, I was so bold as to try that ;-)
>
>Only to discover that there is hardly any documentation on how to access
>stuff... now I, myself am more a C-programmer, and I know C++ is pretty
>much different... it is really hard for me to think in classes and stuff,
>to think object-oriented...
>
>So, now you know I am an earthling :P
>
>I tried this kind of approach, the idea was to start out simple... so don't
>laugh at the attempt ...
>
>#include <pqxx/connection.h>
>
>int main(int argc, char *argv[])
>{
>         Connection *db;
>         char uname[250]; // username
>         char passwd[250]; // password
>         char host[250]; //hostname
>         char passwd[250]; //password
>         char dbase[250]; //database name
>         char connstring[1264];
>         ... some output and input to get values for all these char arrays...
>
>         connstring = "host=" >> host >> "database=" >> dbase >> "username="
> >>
>uname >> "password=" >> passwd;
>         db = new Connection(connstring,true);
>
>         if(db->is_open())
>         {
>                 cout << "Connection succesful!" << endl;
>                 cout << db->Options();
>         }
>         else
>         {
>                 cout << "Something went wrong... oops" << endl;
>         }
>         delete db;
>         return 0;
>}
>
>I get my first errors at "Connection *db" declaration... there is no
>Connection defined...
>Can anyone point out how to reference the classes libpqxx has?
>
>Also I was curious if my connstring was correctly built, but that is
>another subject I think... One other very important thing would be: how can
>I find out the error PostgreSQL will throw at me when I did something ugly?
>At least I can find out the problem, when I make one then...
>
>Michiel
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster



Re: Getting to learn libpqxx

From
"Matt Fitzgerald"
Date:
Try compiling with the C++ compiler:

g++ -o source source.cpp

(You don't need to specify the .h extensions in C++)

-Matt


-----Original Message-----
From: Michiel Lange [mailto:michiel@minas.demon.nl]
Sent: Friday, 11 April 2003 6:59 AM
To: Matt Fitzgerald
Cc: 'Michiel Lange'; pgsql-interfaces@postgresql.org
Subject: Re: [INTERFACES] Getting to learn libpqxx

Ok... here are some answers:

Yes, pqxx/connection.h is in the include path, the errormessage I would get
would be very different...

I compile the source with gcc: gcc source.cpp -o source
the libraries should be in place, I placed them in /usr/local/lib (and ran
ldconfig)

I will try the #include <sstream.h> (I believe you forgot the .h), but from
what I remember the
char foo[234];

foo = bar << "blah" << bla;

would be perfectly valid... but I can be mistaken... I will try your
suggestion and see if it will help...
yet, the problem that the declaration (Connstring *db;) is not valid...

Michiel
At 17:32 10-4-2003 +1000, Matt Fitzgerald wrote:
>Ok... some stupid questions:
>
>1. I take it pqxx/connection.h is in the include path?
>2. If so, what command are you using to compile the source?
>
>Also, your connection string is not built correctly, 'connstring' is not a
>class, thus it doesn't support any overloaded operators such as '<<' & '+'
>etc.
>
>Do some thing like this:
>
>#include <sstream>
>
>std::stringstream connstring;
>
>connstring << "host=" << host << "database=" << dbase << "username=" <<
>uname << "password=" << passwd;
>
>// call connection constructor something like this:
>Connection *db = new Connection(connstring.str().c_str());
>
>
>Etc.. etc.. also, as 'bool Immediate' defaults to 'true' you don't have to
>specify it.
>
>Regards,
>   -Matt
>
>
>
>
>-----Original Message-----
>From: pgsql-interfaces-owner@postgresql.org
>[mailto:pgsql-interfaces-owner@postgresql.org] On Behalf Of Michiel Lange
>Sent: Thursday, 10 April 2003 8:31 AM
>To: pgsql-interfaces@postgresql.org
>Subject: [INTERFACES] Getting to learn libpqxx
>
>Hello everybody again,
>
>I went out to investigate and try if I could master libpqxx, for libpq++
>works not at all. Seeing it is discontinued, and people are encouraged to
>use libpqxx, I was so bold as to try that ;-)
>
>Only to discover that there is hardly any documentation on how to access
>stuff... now I, myself am more a C-programmer, and I know C++ is pretty
>much different... it is really hard for me to think in classes and stuff,
>to think object-oriented...
>
>So, now you know I am an earthling :P
>
>I tried this kind of approach, the idea was to start out simple... so don't
>laugh at the attempt ...
>
>#include <pqxx/connection.h>
>
>int main(int argc, char *argv[])
>{
>         Connection *db;
>         char uname[250]; // username
>         char passwd[250]; // password
>         char host[250]; //hostname
>         char passwd[250]; //password
>         char dbase[250]; //database name
>         char connstring[1264];
>         ... some output and input to get values for all these char
arrays...
>
>         connstring = "host=" >> host >> "database=" >> dbase >>
"username="
> >>
>uname >> "password=" >> passwd;
>         db = new Connection(connstring,true);
>
>         if(db->is_open())
>         {
>                 cout << "Connection succesful!" << endl;
>                 cout << db->Options();
>         }
>         else
>         {
>                 cout << "Something went wrong... oops" << endl;
>         }
>         delete db;
>         return 0;
>}
>
>I get my first errors at "Connection *db" declaration... there is no
>Connection defined...
>Can anyone point out how to reference the classes libpqxx has?
>
>Also I was curious if my connstring was correctly built, but that is
>another subject I think... One other very important thing would be: how can
>I find out the error PostgreSQL will throw at me when I did something ugly?
>At least I can find out the problem, when I make one then...
>
>Michiel
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster



Re: Getting to learn libpqxx

From
jtv@xs4all.nl
Date:
Sorry for chipping in so late--I'm just back from a vacation and trying to
catch up with a few thousand unread emails.  My Internet connection in
Thailand was sporadic at best.  Hope it's still of some use.


> (You don't need to specify the .h extensions in C++)

Actually, that only applies to the standard C++ headers.  These now have
names without the ".h" which may exist side-by-side with, and need not be
compatible with, the ".h" ones.

On to Michiel's message.  I'm afraid many of the problems here are C++
questions, rather than libpqxx-specific problems.  Check out the test
programs provided with libpqxx; if you download the latest Development
version from http://pqxx.tk/, test001.cxx has been cleaned up a lot to
provide a simpler example.  See below for more.


> -----Original Message-----
> From: Michiel Lange [mailto:michiel@minas.demon.nl]

> Yes, pqxx/connection.h is in the include path, the errormessage I would
> get would be very different...

Are you also using the pqxx namespace?  Remember, all symbols defined by
libpqxx are within the pqxx namespace just as cout, string etc. exist only
inside the std namespace.


> foo = bar << "blah" << bla;
>
> would be perfectly valid... but I can be mistaken... I will try your
> suggestion and see if it will help...

Why not just use the + operator?

foo = bar + "blah" + bla;


> yet, the problem that the declaration (Connstring *db;) is not valid...

There is no Connstring class in libpqxx.  Do you mean Connection?  Or did
you define a Connstring

BTW, are you sure you want a pointer?  In many cases it'll be easier to
create a Connection object as a local variable.


>>Only to discover that there is hardly any documentation on how to access
>>stuff... now I, myself am more a C-programmer, and I know C++ is pretty
>>much different... it is really hard for me to think in classes and stuff,
>>to think object-oriented...

Here's the bad news: Object-orientation is no longer where the action is
in C++.  It's moved on to generic programming and whatnot.


>>         db = new Connection(connstring,true);
>>
>>         if(db->is_open())
>>         {
>>                 cout << "Connection succesful!" << endl;
>>                 cout << db->Options();
>>         }
>>         else
>>         {
>>                 cout << "Something went wrong... oops" << endl;
>>         }

This is not needed.  The constructor will throw an exception if it fails. 
Besides, it may defer the actual opening of the connection until it is
actually used.  So all you need to do is:

int main(int argc, char *argv[])
{ try    // (which you should do anyway!) {   // Build connstring...   Connection db(connstring);   // Use db... }
catch(const exception &e) {   cout << e.what() << endl;   return 1; } return 0;
 
}


>>         delete db;
>>         return 0;

This is not the safe way to do it!  If your program throws an exception,
the delete may never be performed.


>>I get my first errors at "Connection *db" declaration... there is no
>>Connection defined...
>>Can anyone point out how to reference the classes libpqxx has?

They're in namespace pqxx.  So you can either write "using namespace
pqxx;" at the head of your program (just like the example programs do) or
refer to Connection etc. as pqxx::Connection etc.  Chances are you'll want
the first option.


>>Also I was curious if my connstring was correctly built, but that is
>>another subject I think... One other very important thing would be: how
>> can
>>I find out the error PostgreSQL will throw at me when I did something
>> ugly?
>>At least I can find out the problem, when I make one then...

It's just a standard exception, derived from C++'s runtime_error class (in
the std namespace).

I'm sorry if this is a lot coming at you at the same time.  C++ really is
a major change from C and there is a lot to learn before you can start to
write serious programs in it, regardless of your existing C knowledge.  It
really has become a pretty language in some ways, but hellishly
complicated in others and it definitely isn't just "A Better C" anymore.


Jeroen