Tag: postgresql

在C中连接Postgresql

我正在尝试将在Windows上运行的C脚本连接到Postgresql数据库。 目前,我在网站上获得了这个脚本: #include #include “libpq-fe.h” #include #include int main() { PGconn *conn; PGresult *res; int rec_count; int row; int col; conn = PQconnectdb(“dbname=ljdata host=localhost user=dataman password=supersecret”); if (PQstatus(conn) == CONNECTION_BAD) { puts(“We were unable to connect to the database”); exit(0); } res = PQexec(conn, “update people set phonenumber=\’5055559999\’ where id=3”); res = PQexec(conn, “select lastname,firstname,phonenumber […]

编译PostgreSQL 9.6的C函数时出错

我正在升级到PostgreSQL 9.6并在尝试编译一些C代码时遇到一些错误。 gcc -c -o lib/libhaver.o src/libhaver.c -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector –param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fPIC -D_GNU_SOURCE -I. -I/usr/pgsql-9.6/include/server -I/usr/pgsql-9.6/include/server/access -I/usr/pgsql-9.6/include/internal -I/usr/include/et -I/usr/include/libxml2 -I/usr/include 这段代码: #include “postgres.h” #include “fmgr.h” #include “catalog/pg_type.h” #include “funcapi.h” #include “utils/guc.h” #include “access/htup.h” #include “utils/array.h” #include “math.h” #ifdef NEWPOSTGRES #include […]

Postgresql用户定义了c函数问题

我在我的ubuntu 14.04中使用apt-get命令安装了postgresql .. postgresql 9.4 libpg 9.4.8 我想为动态加载添加用户定义的c函数。 我按照规范准备好了我的c文件和sql函数文件,但主要问题是c文件包含像…这样的标题行。 #include“postgres.h” #include #include“fmgr.h” 我在桌面上有我的文件夹,但没有postgers.h或fmgr.h文件.. 我不知道在我的系统上找到源文件的位置,但我从git下载了整个源代码并添加到同一个文件夹中.. 如果我运行编译命令它显示 postgres_ext.h:47:9: error: unknown type name ‘PG_INT64_TYPE’ typedef PG_INT64_TYPE pg_int64; 我不知道从哪里开始..我应该将我的文件放在任何postgres目录,然后编译或下载源文件是正确的选项? 怎么处理错误..? 请帮助..非常感谢提前。

错误C2011:’timezone’:’struct’类型重定义(postgres)

我正在努力建立一个。 dll文件用C函数扩展postgres服务器。 我正在使用visual studio 2012构建dll和PostgreSQL 9.2。 我导入所有目录postgres“\ include \ server *”但我有错误: 错误C2011:’timezone’:’struct’类型重新定义 错误C2011:’itimerval’:’struct’类型重新定义 在第205和214行的文件* pg_confi_os.h *中 我试过这个解决方案但没有成功。 如何解决这个问题呢?

如何转储PGresult

我正在尝试调试访问postgres数据库的C程序。 我想使用一些postgres提供的例程来转储PGresult的内容。 是否有一个postgres提供的例程,将以人类可读的方式转储PGresult? 我将添加它作为将转储PGresult内容的日志记录的选项。 以下是非SELECT命令的相关问题: 如何为非SELECT命令转储PGresult

如何为非SELECT命令转储PGresult

我正在尝试调试访问postgres数据库的C程序。 我想使用一些postgres提供的例程来转储PGresult的内容,其中PGresult来自一个不是SELECT的命令。 是否有一个postgres提供的例程,将以人类可读的方式转储PGresult? 我将添加它作为将转储PGresult内容的日志记录的选项。 我之前问过这个问题: 如何转储PGresult ,答案对SELECTs有好处,但对其他命令不好。 [edit]我希望将其转储以进行调试。 因此,如果我将日志记录设置为调试级别,我希望看到PGreturn中的任何信息。 我希望有一些function可以做到这一点。 我也希望它能删除不相关的信息。 我没有查看Postgres源代码以查看可用的内容。

使用netbeans定义的GUI在postgreSQL中调用位于“postgres.c”中的用户定义函数

我想在PostgreSQL-8.4.15中添加3个user_defined函数。 这里有3个function: (1) start_create_profile(); (2) make_profile(); (3) check_anomaly(); 所有这些都写在位于src / backend / tcop的文件“test.c”中。 我想从exec_simple_query()的中间调用(1)和(3 exec_simple_query() 。 exec_simple_query()是PostgreSQL函数,它写在位于src / backend / tcop的“postgres.c”中。 我想直接通过我的GUI调用(2)。 这是我在“test.c”中编写的代码: #include “postgres.h” #ifndef PROGPROFILE_H_ #define PROGPROFILE_H_ /* interfaces */ extern void start_create_profile(List *querytree_list); extern void create_profile(); extern void check_anomaly(List *querytree_list); #endif /* Test ProgProf */ void start_create_profile(List *querytree_list){ ListCell *l; ListCell *tl; […]

Postgres C函数 – 传递和返回数字

我刚刚开始使用Postgres外部C函数进行测试。 当我传入一个数字并返回它时,该function正常。 (例) 样本函数 PG_FUNCTION_INFO_V1(numericTesting); Datum numericTesting(PG_FUNCTION_ARGS) { Numeric p = PG_GETARG_NUMERIC(0); PG_RETURN_NUMERIC(p); } 但是,当我尝试对传入的变量执行任何数学函数时,它将无法编译。 我明白了 错误:二进制文件的操作数无效* 样本函数 PG_FUNCTION_INFO_V1(numericTesting); Datum numericTesting(PG_FUNCTION_ARGS) { Numeric p = PG_GETARG_NUMERIC(0); PG_RETURN_NUMERIC(p * .5); } 是什么造成的? 我猜测Numeric数据类型需要一些函数来允许数学。 我尝试使用:PG_RETURN_NUMERIC(DatumGetNumeric(p * .5)),但结果相同。

将C变量传递给SQL命令

我是使用libpq并在postgresql数据库上工作的新手。 所以,我可以使用C程序插入/更新/等一个postgresql数据库,只要我在引号内给出实际值。 我想知道如何在命令中传递字符串/整数变量?? 例如,以下代码将一个名为“comment”的列添加到现有表“people”中,其中包含“TRUE”默认值。 我需要将“comment”的值更新为“FALSE”,其中id = 2。 #include #include #include #include void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main() { PGconn *conn; PGresult *res; int nFields; int row_count=0,col_count=0; int row=0; int col=0; conn = PQconnectdb(“dbname=test host=localhost user=postgres password=xxx”); if(PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, “Connection to database \”%s\” failed.\n”, PQerrorMessage(conn)); fprintf(stderr, “%s”, PQerrorMessage(conn)); exit_nicely(conn); } […]

C中的等效plpgsql触发器

我有一个PostgreSQL 9.0服务器,我在一些表上使用遗产,因此我必须通过这样的触发器来模拟外键: CREATE OR REPLACE FUNCTION othertable_before_update_trigger() RETURNS trigger AS $BODY$ DECLARE sql VARCHAR; rows SMALLINT; BEGIN IF (NEW.parenttable_id IS DISTINCT FROM OLD.parenttable_id) THEN sql := ‘SELECT id ‘ || ‘FROM parentTable ‘ || ‘WHERE id = ‘ || NEW.parenttable_id || ‘;’; BEGIN EXECUTE sql; GET DIAGNOSTICS rows = ROW_COUNT; EXCEPTION WHEN OTHERS THEN RAISE […]