Hello again,

Is there a good tutorial for libpqxx around?  The only one I've found is the
one it comes with (also linked on pqxx.org).  Unfortunately this tutorial
doesn't cover very much.  Without knowing quite what I'm looking for, it's
hard to locate things in the Doxygen reference.

I'm wondering about alternative ways to insert data.  In the libpqxx tests,
there are plenty of examples that insert e.g. a 2D array of strings, with an
assumed field order.  They all use pqxx::work::insert().

What I'd like to do is explicitly state the fields I'm inserting.  I'd rather
not rely on the DB field order being fixed, and I'd like to leverage default
values for fields where they exist, etc.  I'd also like to use prepared
statements for inserts so that I don't have to worry about quoting and
stringifying everything myself.

I set up a prepared statement to handle an insert, and it worked fine..except
result.inserted_oid() returned 0.  The row was actually inserted in the DB,
and result.affected_rows() returned 1.

Here's my prepared statement:

dbHandle->prepare("saveTargetStmt",
  "insert into nac_targets (image_id, execution_time, exposure_time, lines, "
  "compander_select, dac_reset_level_left, dac_reset_level_right, "
  "channel_a_offset_left, channel_a_offset_right, "
  "channel_b_offset_left, channel_b_offset_right) "
  "values ($1, now(), $2, $3, $4, $5, $6, $7, $8, $9, $10)")
  ("integer", pqxx::prepare::treat_direct)("integer", 
pqxx::prepare::treat_direct)
  ("integer", pqxx::prepare::treat_direct)("integer", 
pqxx::prepare::treat_direct)
  ("integer", pqxx::prepare::treat_direct)("integer", 
pqxx::prepare::treat_direct)
  ("integer", pqxx::prepare::treat_direct)("integer", 
pqxx::prepare::treat_direct)
  ("integer", pqxx::prepare::treat_direct)("integer", 
pqxx::prepare::treat_direct);

Not shown in the prepared statement is the "id" field, a serial.  That's the
value I was hoping to get from result.inserted_oid().

Summary of questions:

1) Is it possible to obtain the ID of a row inserted via a prepared statement?
2) If so, is there some reason it's not showing up in inserted_oid()?
3) If not, what's the right way to specify a hash of field/values for insert?
   Do I have to handle all the type conversions myself?
4) Any pointers to more complete tutorials on general pqxx usage?

Thanks,
Jim
_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@gborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to