Here's my entire test program.  I'm using 8.2.3 postgres and 2.6.9
libpqxx.  I'm trying to break up the loading of a prepared statement
into individual steps so I can write generic code to dynamically
populate any prepared statement.  I show two attempts below.  Each
fails with a different exception.  Any suggestions?  I'll try looking
at the libpqxx code...




#include <iostream>

#include <pqxx/pqxx>

using namespace PGSTD;
using namespace pqxx;


int main()
{
  try {
    connection C;
    work *T;
    T = new work(C, "test1");

    try {
      T->exec("drop table x");
    }
    catch (const sql_error &e) {
      cerr << "Drop failed." << endl;
      T->abort();
      delete T;
      T = new work(C, "test1");
      cerr << "New transaction." << endl;
    }

    T->exec("create table x (num numeric)");
    T->exec("insert into x (num) values (111)");  // This works.

    C.prepare("p1", "insert into x (num) values ($1)")
      ("varchar", prepare::treat_string);

    T->prepared("p1")(222).exec();  // This works.

    // 1st attempt
    //prepare::invocation Inv = T->prepared("p1");
    //Inv("333");
    //Inv.exec();  // Exception: Unknown prepared statement '333'

    // 2nd attempt
    prepare::invocation Inv = T->prepared("p1")(333);
    Inv.exec();  // Exception: Unknown prepared statement 'p1

    T->commit();
  }
  catch (const sql_error &e)
  {
    cerr << "SQL error: " << e.what() << endl
         << "Stmt was: " << e.query() << endl;
    return 1;
  }
  catch (const exception &e)
  {
    cerr << "Exception: " << e.what() << endl;
    return 2;
  }
  catch (...)
  {
    cerr << "Unhandled exception" << endl;
    return 100;
  }

  return 0;
}
_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@gborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to