当我通过 libpq
执行 SQL 命令时,如果出现类似这样的错误,它会报告错误消息。
Error Domain=PQConnection Code=1 "ERROR: syntax error at or near "fef"
LINE 1: fef
^
这是字符串消息。我可以解析消息——因为它非常有规律——以获取行号/列号,但这是不可靠的。是否有任何其他 API 来获取行号/列号?任何正则化整数形式都很好。
最佳答案
使用 PQresultErrorField
函数和键 PG_DIAG_STATEMENT_POSITION
。
来自 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.
关于postgresql - 如何从 SQL 语法错误或从 libpq 执行异常中获取错误行/列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524518/