I have a database which is called mydb
with a table People(id, name)
.
I want to insert a row to this table using pqxx C++ interface.
The SQL query is quite simple INSERT INTO people (id, name) VALUES (1, "Bob");
The C++ code is here:
#include <pqxx/pqxx>
using namespace std;
int main(int, char *argv[])
{
pqxx::connection conn( /* some stuff here */ );
conn.prepare("insert_to_people", "insert into people (id, name) values ($1, $2);");
pqxx::work pq(conn);
pq.prepared("insert_to_people")(1)("Bob").exec();
pq.commit();
return 0;
}
But I get the following exception:
terminate called after throwing an instance of 'pqxx::usage_error'
what(): Too many arguments for prepared statement insert_to_people: expected 0, received 2
Aborted (core dumped)
What is wrong?
It has been a few years since I used c++! Looking at the doc it seems that the argument types need to be declared? Maybe this will work:
conn.prepare("insert_to_people", "insert into people (id, name) values ($1, $2)")("integer")("varchar", pqxx::prepare::treat_string);
Also, there was a trailing ; in the query. I don't think it will hurt but it is not needed.
-g