0

I'm new to postgresql and would like to ask how to do a transaction using BEGIN, COMMIT and PQexecPrepared. In my program, I have to update many tables before COMMIT. I do understand that we need to use:

1. PQexec(conn,"BEGIN");
2. execute some queries
3. PQexec(conn,"COMMIT");

I had first tried by using PQexecParams, it worked:

PQexec(conn,"BEGIN");
PQexecParams(conn,  "INSERT INTO Cars (Id,Name, Price) VALUES ($1,$2,$3)", 
                     3, NULL, parValues, NULL , NULL, 0 );
PQexec(conn,"COMMIT");

However when I had tried by using PQexecPrepared, my table Cars wasn't updated after COMMIT (of course it worked in autocommit mode without BEGIN &COMMIT )

PQexec(conn,"BEGIN");
PQprepare(conn,"teststmt",  "INSERT INTO Cars (Id,Name, Price) VALUES ($1,$2,$3)", 3, NULL );
PQexecPrepared(conn, "teststmt", 3, parValues,NULL, NULL,0);   
PQexec(conn,"COMMIT");

Do you have any advice in this case?

muocdich
  • 41
  • 1
  • 8

0 Answers0