我创建了一个c函数,该函数在我的postgresql数据库上创建了一个表。在这里:
#include <stdio.h>
#include <libpq-fe.h>
#include <stdlib.h>
PGconn *connection;
PGresult *re;
void create_table(){
re= PQexec(connection, "SELECT to_regclass('fp_stores_data')");
if(!re){
PQclear(re);
re = PQexec(connection ,"CREATE TABLE test(name VARCHAR(3))");
if(PQresultStatus(re)==PGRES_COMMAND_OK){
printf("table created!");
}else{
printf("failed to create table!");
}
}else{
printf("table was created befor!");
}
PQclear(re);
}
int main() {
connection = PQconnectdb("user=username password=123 dbname=projectdb");
create_table();
PQfinish(connection);
return 0;
}
但是每次我运行程序时,它都会打印failed to create table!
,并且该表名在数据库中将没有任何内容。
答案 0 :(得分:3)
使用PQresultErrorMessage
来获取您执行的SQL的错误文本,例如:
printf("%s\n", PQresultErrorMessage(re));
或PQerrorMessage
获取与您的连接相关的任何错误的错误文本,例如:
printf("%s\n", PQerrorMessage(connection));
这些功能的postgresql文档在这里: https://www.postgresql.org/docs/current/libpq-exec.html