Thread: libpq question...

libpq question...

From
wy2lam@yahoo.com (Michael Lam)
Date:
Hello,

I have a question about insertions with duplicate primary keys...

I was trying to detect duplicated rows while inserting rows with libpq
making calls to stored procedures.

Suppose I have the following snipplet:

PGresult *res;
res = PQexec(conn, "SELECT AddRow(1);"
if (!res) {
    // connection bad
    return CONNECTION_BAD;
}
if (PGRES_TUPLES_OK != PQresultStatus(res)) {
    string errMsg = PQerrorMessage(conn);
    if (string::npos != errMsg.find("dupl")) {
        return DUPLICATED_ROWS;
    } else {
        return CONNECTION_BAD;
    }
}

Question is: is there a better way of detecting duplicated rows than
checking for "duplicate" in the error message?  I fear the error
string may get changed and break the code...

I have tried looking at the PQresultStatus(res), but unfortunately it
is PGRES_FATAL_ERROR - which is the same as when I shut down the
backend or a disconnection - and I want to do different things for
duplications and disconnections.

Can anyone shed some light on this?  Thanks!!