Chinaunix首页 | 论坛 | 博客
  • 博客访问: 830454
  • 博文数量: 137
  • 博客积分: 3477
  • 博客等级: 中校
  • 技术积分: 1409
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-30 21:53
文章分类

全部博文(137)

文章存档

2024年(8)

2023年(10)

2022年(4)

2021年(10)

2020年(9)

2018年(1)

2017年(1)

2014年(4)

2013年(3)

2012年(12)

2011年(24)

2010年(2)

2009年(8)

2008年(6)

2007年(34)

2006年(1)

分类: Mysql/postgreSQL

2021-03-29 20:49:18

有关 ecpg 的功能方面的内容,请自行查阅 postgresql 手册。

测试建库语句:

点击(此处)折叠或打开

  1. create database sampledb;

测试表建表语句:

点击(此处)折叠或打开

  1. create table item_info (
  2.       km_code varchar(9),
  3.       org_id varchar(19),
  4.       km_name varchar(33),
  5.       km_level int2,
  6.       km_code_parent varchar(9),
  7.       jd_type int2,
  8.       km_attr int2,
  9.       data_dt date,
  10.       constraint pk_item_info primary key(km_code)
  11. )

建立连接数据库测试:pg_connect.pgc

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main ( void )
  4. {
  5. EXEC SQL BEGIN DECLARE SECTION;
  6. char *target="sampledb@192.168.137.208:5432";
  7. char *user="postgres";
  8. char *passwd="postgres";
  9. EXEC SQL END DECLARE SECTION;


  10.         exec sql connect to :target user :user using :passwd;

  11.         printf("%s\n",target);

  12.         if (sqlca.sqlcode == 0 )
  13.                 printf("connect sucesess ... \n");
  14.         else {
  15.                 printf("connect faild ... ... \n");
  16.                 printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
  17.         }

  18.         exit(0);
  19. }
插入数据测试:pg_insert.pgc

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main ( void )
  4. {
  5.         EXEC SQL BEGIN DECLARE SECTION;
  6.         char *target="sampledb@192.168.137.208:5432";
  7.         char *user="postgres";
  8.         char *passwd="postgres";

  9.         char km_code[9] = "1909";
  10.         char org_id[19] = "8001";
  11.         char km_name[33] = "bbbb";
  12.         int km_level = 1;
  13.         char km_code_parent[9] = "0000";
  14.         int jd_type = 1;
  15.         int km_attr = 1;
  16.         char data_dt[11] = "2020-12-31";


  17.         EXEC SQL END DECLARE SECTION;


  18.         exec sql connect to :target user :user using :passwd;

  19.         printf("%s\n",target);

  20.         if (sqlca.sqlcode != 0 ) {
  21.                 printf("connect faild ... ... \n");
  22.                 printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
  23.                 exit(0);
  24.         }

  25.         exec sql begin;
  26.         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 ) ;
  27.         if ( sqlca.sqlcode == 0 ) {
  28.                 exec sql commit;
  29.                 printf (" insert into item_info ok \n");
  30.         } else {
  31.                 printf (" insert into item_info err , sqlca.sqlcode = %d \n" , sqlca.sqlcode );
  32.                 exec sql rollback;
  33.         }
  34.         exit(0);
  35. }
检索数据 pg_select.pgc

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main ( void )
  4. {
  5.         EXEC SQL BEGIN DECLARE SECTION;
  6.         char *target="sampledb@192.168.137.208:5432";
  7.         char *user="postgres";
  8.         char *passwd="postgres";

  9.         char km_code[9];
  10.         char org_id[19];
  11.         char km_name[33];
  12.         int km_level;
  13.         char km_code_parent[9];
  14.         int jd_type;
  15.         int km_attr;
  16.         char data_dt[11];


  17.         EXEC SQL END DECLARE SECTION;


  18.         exec sql connect to :target user :user using :passwd;

  19.         printf("%s\n",target);

  20.         if (sqlca.sqlcode != 0 ) {
  21.                 printf("connect faild ... ... \n");
  22.                 printf( " sqlca.sqlcode = %d \n",sqlca.sqlcode );
  23.                 exit(0);
  24.         }

  25.         exec sql select km_code,org_id, km_name , km_level , km_code_parent , jd_type , km_attr , data_dt
  26.                 into :km_code , :org_id , :km_name , :km_level, :km_code_parent, :jd_type, :km_attr, :data_dt from item_info while limit 1;

  27.         if ( sqlca.sqlcode == 0 )
  28.         {
  29.                 printf(" km_code = %s \n",km_code);
  30.                 printf(" org_id = %s \n",org_id);
  31.                 printf(" km_name = %s \n",km_name);
  32.                 printf(" km_level = %d \n",km_level);
  33.                 printf(" km_code_parent = %s \n",km_code_parent);
  34.                 printf(" jd_type = %d \n",jd_type);
  35.                 printf(" km_attr = %d \n",km_attr);
  36.                 printf(" data_dt = %s \n",data_dt);
  37.         }
  38.         exit(0);
  39. }
Makefile 文件:

点击(此处)折叠或打开

  1. TARGET = pg_connect pg_select pg_insert
  2. CC = gcc
  3. CFLAGS = -g -O2 -c -fPIC -DDEBUG
  4. ECPG = ecpg -c
  5. AR = ar -rcvl
  6. RANLIB = ranlib
  7. INCLDIR = -I. -I/opt/PostgreSQL/11.10/include
  8. LIBDIR = -L. -L$(HOME)/lib -L/opt/PostgreSQL/11.10/lib
  9. LIBS = -lecpg
  10. OBJ =
  11. all:$(TARGET)
  12. clean:
  13. rm -f $(TARGET) *.o core.* .*.swp
  14. pg_connect:pg_connect.o
  15. $(CC) -o $@ $^ $(LIBDIR) $(LIBS)
  16. pg_select:pg_select.o
  17. $(CC) -o $@ $^ $(LIBDIR) $(LIBS)
  18. pg_insert:pg_insert.o
  19. $(CC) -o $@ $^ $(LIBDIR) $(LIBS)
  20. # ################################################################################
  21. .SUFFIXES:
  22. .SUFFIXES:.c .o .pgc
  23. .pgc.c:
  24. $(ECPG) $(INCLUDE) $<
  25. .c.o:
  26. $(CC) $(CFLAGS) $(INCLDIR) $<

需要注意:
以上参个程序编译完成后,运行时需要 /usr/lib/libecpg.so.6 和 /usr/lib/libpgtypes.so.3 这两个动态连接库。
切记!切记!

阅读(929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~