无法使用C中的libpq在数据库中创建表
我创建了一个c函数,用于在postgresql数据库上创建一个表。在这里:无法使用C中的libpq在数据库中创建表,c,database,postgresql,libpq,C,Database,Postgresql,Libpq,我创建了一个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')&q
#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;
}
#包括
#包括
#包括
PGconn*连接;
PGresult*re;
void创建_表(){
re=PQexec(连接,“选择到\u regclass('fp\u存储\u数据')”);
如果(!re){
PQclear(re);
re=PQexec(连接,“创建表测试(名称VARCHAR(3))”);
如果(PQresultStatus(re)=PGRES\u命令\u确定){
printf(“创建表!”);
}否则{
printf(“创建表失败!”);
}
}否则{
printf(“表是在之前创建的!”);
}
PQclear(re);
}
int main(){
connection=PQconnectdb(“user=username-password=123 dbname=projectdb”);
创建_表();
PQfinish(连接);
返回0;
}
但每次我运行程序时,它的print
都无法创建表
并且数据库中将没有具有该表名的任何内容。使用pqresultormessage
获取所执行SQL的错误文本,例如:
printf("%s\n", PQresultErrorMessage(re));
或PQerrorMessage
获取与连接相关的任何错误的错误文本,例如:
printf("%s\n", PQerrorMessage(connection));
这些函数的postgresql文档如下:
当您失败时,您应该打印
PQerrorMessage(连接)
,这将为您提供比失败更多的信息。此函数返回什么?还是打印?谢谢!我知道问题与数据库有关。