[Libpqxx-general] Tablewriter: entering specified fields only
Jeroen Vermeulen
jtv at xs4all.nl
Tue May 19 07:20:47 UTC 2009
Robert Backhaus-pqxx wrote:
> I am having trouble creating and using a tablewriter to enter data in only
> some fields of a table. Replies to this and other lists say it is done by
> specifying a list of column names, but I get only crashes.
>
> Given a table with colums int "machine_id", int "bytecount" and timestamp
> "time" default now(), I would expect to be able to do
> tablewriter W(T, "tablename", "id", "data" );, but it segfaults, with the bt
> below.
>
> What am I doing wrong?
You're assuming the constructor to take varargs, which it doesn't!
If you look at the documentation for the constructor, you'll see that it
takes a sequence of column names. In C++ parlance that means a pair of
iterators (which may also be pointers) to mark the beginning and end of
a variable-length series of column names.
The code you have passes strings where the constructor expects
"something iterable" to mark the beginning/end of the column names.
Normally the compiler would stop you from doing this except you happen
to be passing exactly two column names where the compiler expects
"begincolumns" and "endcolumns." (If you'd passed a third string you
would have been setting a different string to represent null.)
The string arguments become "const char *," which can behave as
iterators over the char array that is your string. The constructor
tries to traverse the range from the first to the second, as it is
documented to do, which in your case causes mayhem.
We could prevent this specific mistake from making it past the compiler
by specializing the constructor for const char * and a few related
types. I've filed this as bug #183.
Jeroen
More information about the Libpqxx-general
mailing list