如何使用 ecpg

本节讨论如何使用 ecpg 工具.

预编译器

预编译器叫 ecpg.在安装过后它存放在 Postgres bin/ 目录下面.

库

ecpg 库叫做 libecpg.a 或 libecpg.so.另外,该库用了 libpq 库与 Postgres 服务器通讯,所以你要将你的程序与这两个库链接: -lecpg -lpq.

库里面有一些方法是"隐藏"的,但是有时候这些方法可能提供非常有用的信息.

错误控制

要想检测从 Postgres 服务器来得错误,你要包含如下一行
      exec sql include sqlca;
到你的文件的包含段里.这样做将会定义一个结构和一个象下面一样名为 sqlca 的变量:
struct sqlca
{
 char sqlcaid[8];
 long sqlabc;
 long sqlcode;
 struct
 {
  int sqlerrml;
  char sqlerrmc[70];
 } sqlerrm;
 char sqlerrp[8];
 long sqlerrd[6];
 /* 0: empty                                         */
 /* 1: OID of processed tuple if applicable          */
 /* 2: number of rows processed in an INSERT, UPDATE */
 /*    or DELETE statement                           */
 /* 3: empty                                         */
 /* 4: empty                                         */
 /* 5: empty                                         */
 char sqlwarn[8];
 /* 0: set to 'W' if at least one other is 'W'       */
 /* 1: if 'W' at least one character string          */
 /*    value was truncated when it was               */
 /*    stored into a host variable.                  */
 /* 2: empty                                         */
 /* 3: empty                                         */
 /* 4: empty                                         */
 /* 5: empty                                         */
 /* 6: empty                                         */
 /* 7: empty                                         */
 char sqlext[8];
} sqlca;
如果最后一个SQL 语句发生了错误,那么 sqlca.sqlcode 将是非零值.如果 sqlca.sqlcode 小于 0 那么就是发生了某种严重的错误,象数据库定义与查询定义不一致等.如果大于 0 则是通常的错误,象表不包括所要求的行等.

sqlca.sqlerrm.sqlerrmc 将包含一个字符串描述该错误.该字符串以源文件的行号结尾。

可能发生的错误列表:
 
-12, Out of memory in line %d.
通常不出现这个错误。这是你的虚拟内存耗尽的标志。
-200, Unsupported type %s on line %d.
通常不出现这个错误.这表明预编译器生成了一些库(函数)不认得的东西.可能你运行的预编译器和当前库不兼容.
-201, Too many arguments line %d.
这意味着 Postgres 返回了比我们的匹配变量更多的参数.可能你漏了几个INTO :var1,:var2-列表里的宿主变量.
-202, Too few arguments line %d.
这意味着 Postgres 返回了比我们的对应宿主变量要少的参数.可能你多输入了几个INTO :var1,:var2-列表里的宿主变量.
-203, Too many matches line %d.
这意味着查询返回了多个行,但你声明的变量不是数组.你执行的 SELECT 可能不是唯一的.
-204, Not correctly formatted int type: %s line %d.
这意味着宿主变量是一个 int 类型并且 Postgres 数据库里的字段是另一种类型,包含着一个不能转换成一个 int 类型的数值.库(函数)使用 strtol 做此类转换.
-205, Not correctly formatted unsigned type: %s line %d.
这意味着宿主变量是一个 unsigned int(无符号整数)类型而Postgres 数据库里的字段是另外一种类型并且包含一个不能转换成unsigned int 的数值.库(函数)使用 strtoul 做这类转换.
-206, Not correctly formatted floating point type: %s line %d.
这意味着宿主变量是一个 float (浮点)类型而 Postgres 数据库里的字段是另外一种类型并且包含一个不能转换成float 的数值.库(函数)使用 strtod 做这类转换.
-207, Unable to convert %s to bool on line %d.
这意味着宿主变量是一个 bool (布尔)类型,而 Postgres 数据库里的字段值既不是 't' 也不是 'f'。
-208, Empty query line %d.
Postgres 返回 PGRES_EMPTY_QUERY,可能的原因是该查询实际上是空的。
-220, No such connection %s in line %d.
程序试图访问一个不存在的联接。
-221, Not connected in line %d.
程序试图访问一个存在的,但是没有打开的联接。
-230, Invalid statement name %s in line %d.
你试图使用的语句还没准备好。
-400, Postgres error: %s line %d.
某种 Postgres 错误。该消息包含来自 Postgres 后端的信息。
-401, Error in transaction processing line %d.
Postgres 给我们的信号,表明我们无法开始,提交或者回卷该事务。
-402, connect: could not open database %s.
与数据库的联接无法工作。
100, Data not found line %d.
�