postgresql - 如何从 SQL 语法错误或从 libpq 执行异常中获取错误行/列号?

标签 postgresql error-handling libpq

当我通过 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/

相关文章:

android - 在 Android 上只捕获 ENOSPC

postgresql - 如何在安装时修复 Rust 柴油 cli 链接 libpq.lib 错误

postgresql - libpq:如何在 PGconn 连接失败后获取错误代码

mysql - Serializable 隔离级别是否保护数据库免受 ACIDRain 攻击?

postgresql - 由同一组的用户拥有的 ALTER postgres 表

postgresql - GeoServer 不会写入我的 PostgreSQL 可更新 View

python - 需要帮助将ibkr API限制为50msg/min

linux - 期望错误处理 - spawn id 未打开

windows - 如果一个进程建立了多个 PostgreSQL 连接并在没有关闭它们的情况下终止,会发生什么?

oracle - Liquibase:为新数据库添加变更集