eonil
eonil

Reputation: 85975

How to get error line/column number from SQL syntax error or executing exception from libpq?

When I execute a SQL command via libpq, it reports error message if there's some error something like this.

Error Domain=PQConnection Code=1 "ERROR:  syntax error at or near "fef"
LINE 1: fef
        ^

This is string message. I can parse the message - because it's very regular - to get line/column number, but that's unreliable. Is there any other API to get line/column numbers? Any regularized integer form would be nice.

Upvotes: 1

Views: 1217

Answers (1)

eonil
eonil

Reputation: 85975

Use PQresultErrorField function with key PG_DIAG_STATEMENT_POSITION.

From manual

PG_DIAG_STATEMENT_POSITION A string containing a decimal integer indicating an error cursor position as an index into the original statement string. The first character has index 1, and positions are measured in characters not bytes.

Upvotes: 1

Related Questions