有关 ecpg 的功能方面的内容,请自行查阅 postgresql 手册。
测试建库语句:
-
create database sampledb;
测试表建表语句:
-
create table item_info (
-
km_code varchar(9),
-
org_id varchar(19),
-
km_name varchar(33),
-
km_level int2,
-
km_code_parent varchar(9),
-
jd_type int2,
-
km_attr int2,
-
data_dt date,
-
constraint pk_item_info primary key(km_code)
-
)
建立连接数据库测试:pg_connect.pgc
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main ( void )
-
{
-
EXEC SQL BEGIN DECLARE SECTION;
-
char *target="sampledb@192.168.137.208:5432";
-
char *user="postgres";
-
char *passwd="postgres";
-
EXEC SQL END DECLARE SECTION;
-
-
-
exec sql connect to :target user :user using :passwd;
-
-
printf("%s\n",target);
-
-
if (sqlca.sqlcode == 0 )
-
printf("connect sucesess ... \n");
-
else {
-
printf("connect faild ... ... \n");
-
printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
-
}
-
-
exit(0);
-
}
插入数据测试:pg_insert.pgc
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main ( void )
-
{
-
EXEC SQL BEGIN DECLARE SECTION;
-
char *target="sampledb@192.168.137.208:5432";
-
char *user="postgres";
-
char *passwd="postgres";
-
-
char km_code[9] = "1909";
-
char org_id[19] = "8001";
-
char km_name[33] = "bbbb";
-
int km_level = 1;
-
char km_code_parent[9] = "0000";
-
int jd_type = 1;
-
int km_attr = 1;
-
char data_dt[11] = "2020-12-31";
-
-
-
EXEC SQL END DECLARE SECTION;
-
-
-
exec sql connect to :target user :user using :passwd;
-
-
printf("%s\n",target);
-
-
if (sqlca.sqlcode != 0 ) {
-
printf("connect faild ... ... \n");
-
printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
-
exit(0);
-
}
-
-
exec sql begin;
-
exec sql insert into item_info ( km_code,org_id, km_name , km_level , km_code_parent , jd_type , km_attr , data_dt ) values (:km_code , :org_id , :km_name , :km_level, :km_code_parent, :jd_type, :km_attr, :data_dt ) ;
-
if ( sqlca.sqlcode == 0 ) {
-
exec sql commit;
-
printf (" insert into item_info ok \n");
-
} else {
-
printf (" insert into item_info err , sqlca.sqlcode = %d \n" , sqlca.sqlcode );
-
exec sql rollback;
-
}
-
exit(0);
-
}
检索数据 pg_select.pgc
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main ( void )
-
{
-
EXEC SQL BEGIN DECLARE SECTION;
-
char *target="sampledb@192.168.137.208:5432";
-
char *user="postgres";
-
char *passwd="postgres";
-
-
char km_code[9];
-
char org_id[19];
-
char km_name[33];
-
int km_level;
-
char km_code_parent[9];
-
int jd_type;
-
int km_attr;
-
char data_dt[11];
-
-
-
EXEC SQL END DECLARE SECTION;
-
-
-
exec sql connect to :target user :user using :passwd;
-
-
printf("%s\n",target);
-
-
if (sqlca.sqlcode != 0 ) {
-
printf("connect faild ... ... \n");
-
printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
-
exit(0);
-
}
-
-
exec sql select km_code,org_id, km_name , km_level , km_code_parent , jd_type , km_attr , data_dt
-
into :km_code , :org_id , :km_name , :km_level, :km_code_parent, :jd_type, :km_attr, :data_dt from item_info while limit 1;
-
-
if ( sqlca.sqlcode == 0 )
-
{
-
printf(" km_code = %s \n",km_code);
-
printf(" org_id = %s \n",org_id);
-
printf(" km_name = %s \n",km_name);
-
printf(" km_level = %d \n",km_level);
-
printf(" km_code_parent = %s \n",km_code_parent);
-
printf(" jd_type = %d \n",jd_type);
-
printf(" km_attr = %d \n",km_attr);
-
printf(" data_dt = %s \n",data_dt);
-
}
-
exit(0);
-
}
Makefile 文件:
-
TARGET = pg_connect pg_select pg_insert
-
-
CC = gcc
-
CFLAGS = -g -O2 -c -fPIC -DDEBUG
-
-
ECPG = ecpg -c
-
-
AR = ar -rcvl
-
RANLIB = ranlib
-
-
INCLDIR = -I. -I/opt/PostgreSQL/11.10/include
-
LIBDIR = -L. -L$(HOME)/lib -L/opt/PostgreSQL/11.10/lib
-
LIBS = -lecpg
-
-
OBJ =
-
-
all:$(TARGET)
-
-
clean:
-
rm -f $(TARGET) *.o core.* .*.swp
-
-
pg_connect:pg_connect.o
-
$(CC) -o $@ $^ $(LIBDIR) $(LIBS)
-
-
pg_select:pg_select.o
-
$(CC) -o $@ $^ $(LIBDIR) $(LIBS)
-
-
pg_insert:pg_insert.o
-
$(CC) -o $@ $^ $(LIBDIR) $(LIBS)
-
-
# ################################################################################
-
-
.SUFFIXES:
-
.SUFFIXES:.c .o .pgc
-
-
.pgc.c:
-
$(ECPG) $(INCLUDE) $<
-
-
.c.o:
-
$(CC) $(CFLAGS) $(INCLDIR) $<
需要注意:
以上参个程序编译完成后,运行时需要 /usr/lib/libecpg.so.6 和 /usr/lib/libpgtypes.so.3 这两个动态连接库。
切记!切记!
阅读(922) | 评论(0) | 转发(0) |