我正在使用postgresql C API使用复制功能将二进制数据推送到表中。
我找回错误:错误:从stdin复制失败:\ n内容:复制texttest,第4行\ n
我的表很简单:CREATE TABLE texttest(atext TEXT);
我组装的二进制流看起来像这样(3行):
0000000 P G C O P Y \n 377 \r \n \0 \0 \0 \0 \0 \0 0000010 \0 \0 \0 \0 001 \0 \0 \0 004 a a a a \0 001 \0 0000020 \0 \0 004 b b b b \0 001 \0 \0 \0 004 c c c 0000030 c 377 377
我的代码如下:
PGconn* _pg_conn = PQconnectdb(...);
PQsendQueryParams(_pgconn, "COPY texttest(atext) FROM STDIN (FORMAT binary)", ...)
PQconsumeInput(_pg_conn);
PGresult * pRes = PQgetResult(_pg_conn);
int status = PQresultStatus(pRes);
if (status != PGRES_COPY_IN) {
// error handling
}
// buffer is a std::string with content as displayed above
int rc = PQputCopyData(_pg_conn, buffer.data(), buffer.size());
if (rc != 1) {
// error handling
}
if (PQputCopyEnd(_pg_conn, nullptr) <= 0 || PQflush(_pg_conn)) {
// error handling
}
PGresult * pRes = PQgetResult(_pg_conn);
int status = PQresultStatus(pRes); // this is now PGRES_FATAL_ERROR
std::string errmsg = PQerrorMessage(_pg_conn)); // and above error message
我注意到消息中报告的行号始终是最后一行(例如,如果我不发送任何行,则会在第1行出现错误)。
知道我在做什么错吗?建议尝试的东西?