[Libpqxx-general] .exec + operator?

Jeroen Vermeulen jtv at xs4all.nl
Sat Sep 26 10:59:46 UTC 2009


Caleb Cushing wrote:
>         tran.exec("DROP SCHEMA IF EXISTS korama CASCADE;"
>                 "CREATE SCHEMA korama;");
> why's that work?
> 
> but this
> 
>         tran.exec("DROP SCHEMA IF EXISTS korama CASCADE;" +
>                 "CREATE SCHEMA korama;");
> 
> doesn't?

Because you're adding two C-style strings.  So it's one const char * 
plus another const char *.  Adding two pointers doesn't work.

This has _nothing_ to do with exec.  It's a basic C++ mistake before you 
even get to the exec call.  However the exec call has another problem: 
please don't put multiple statements in a single exec.  One statement at 
a time.  If you want to have a sequence of statements sent to the server 
in a single go, use the pipeline class--but most likely you won't need it.


> in the tutorial an example looks like this
> 	T.exec("DELETE FROM " + Table + " WHERE ID=" + ID);
> 
> I realize it's concatenating variables but shouldn't it work for just
> straight strings?

Table is a C++ string object, with its own + operators defined.  It 
defines that const char * plus string produces a string.  Then the 
"WHERE" part adds a const char * to that string, which is fine and 
produces another string.


Jeroen


More information about the Libpqxx-general mailing list