↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

已结束支持的版本: 6.5 / 6.3
历史版本。 PostgreSQL 6.3 已结束支持。 请参阅 当前版本手册.

41.3. 查询执行函数

  • PQexec 向 Postgres 提交一个查询。查询成功则返回 PGresult 指针,否则返回 NULL。如果返回了 NULL,可以用 PQerrorMessage 获取该错误的更多信息。

    PGresult *PQexec(PGconn *conn,
                     char *query);

    PGresult 结构封装了后端返回的查询结果。libpq 程序员应注意维护 PGresult 的抽象。请使用下面描述的访问函数检索查询结果。避免直接引用 PGresult 结构的字段,因为它们将来可能改变。

  • PQresultStatus 返回查询的结果状态。PQresultStatus 可以返回下列值之一:

    PGRES_EMPTY_QUERY,
    PGRES_COMMAND_OK,  /* the query was a command */
    PGRES_TUPLES_OK,  /* the query successfully returned tuples */
    PGRES_COPY_OUT,
    PGRES_COPY_IN,
    PGRES_BAD_RESPONSE, /* an unexpected response was received */
    PGRES_NONFATAL_ERROR,
    PGRES_FATAL_ERROR

    如果结果状态为 PGRES_TUPLES_OK,就可以使用下面的例程检索查询返回的元组。

  • PQntuples 返回查询结果中的元组(实例)数。

    int PQntuples(PGresult *res);
  • PQnfields 返回查询结果中的字段(属性)数。

    int PQnfields(PGresult *res);
  • PQfname 返回与给定字段编号相关联的字段(属性)名。字段编号从 0 开始。

    char *PQfname(PGresult *res,
                  int field_index);
  • PQfnumber 返回与给定字段名相关联的字段(属性)编号。

    int PQfnumber(PGresult *res,
                  char* field_name);
  • PQftype 返回与给定字段编号相关联的字段类型。返回的整数是该类型的内部编码。字段编号从 0 开始。

    Oid PQftype(PGresult *res,
                int field_num);
  • PQfsize 返回与给定字段编号相关联的字段的大小,以字节计。如果返回的大小为 -1,则该字段是变长字段。字段编号从 0 开始。

    int2 PQfsize(PGresult *res,
                 int field_index);
  • PQgetvalue 返回字段(属性)值。对大多数查询而言,PQgetvalue 返回的是属性值的以空字符结尾的 ASCII 字符串表示。如果查询是 BINARY 游标的结果,那么 PQgetvalue 返回的就是该类型以后端服务器内部格式表示的二进制表示。此时由程序员负责把数据转换成正确的 C 类型。PQgetvalue 返回的值指向属于 PGresult 结构的存储空间。如果需要在 PGresult 结构本身的生存期结束后继续使用该值,就必须显式地将它复制到其他存储空间。

    char* PQgetvalue(PGresult *res,
                     int tup_num,
                     int field_num);
  • PQgetisnull 测试一个字段是否为 NULL 条目。

    int PQgetisnull(PGresult *res,
                    int tup_num,
                    int field_num);

    如果字段包含 NULL,此函数返回 1;包含已知值则返回 0。

  • PQgetlength 返回字段(属性)的长度,以字节计。如果字段是 struct varlena,这里返回的长度不含 varlena 的大小字段,即少 4 字节。

    int PQgetlength(PGresult *res,
                    int tup_num,
                    int field_num);
  • PQcmdStatus 返回与最后一条查询命令相关联的命令状态。

    char *PQcmdStatus(PGresult *res);
  • PQcmdTuples 返回受最后一条命令影响的行数。

    const char *PQcmdTuples(PGresult *res);

    如果最后一条命令是 INSERT、UPDATE 或 DELETE,此函数返回一个包含受影响行数的字符串。如果最后一条命令是其他命令,返回空字符串。

  • PQoidStatus 如果最后一条查询是 INSERT 命令,返回一个含有所插入元组对象 id 的字符串。否则返回空字符串。

    char* PQoidStatus(PGresult *res);
  • PQprint 打印出所有的元组以及(可选的)属性名到指定的输出流。

    void PQprint(FILE* fout,      /* output stream */
                 PGresult* res,
                 PQprintOpt* po);
    
    struct _PQprintOpt
    	{
    		pqbool	header;	     /* print output field headings and row count */
    		pqbool	align;	     /* fill align the fields */
    		pqbool	standard;    /* old brain dead format */
    		pqbool	html3;	     /* output html tables */
    		pqbool	expanded;    /* expand tables */
    		pqbool	pager;	     /* use pager for output if needed */
    		char	*fieldSep;   /* field separator */
    		char	*tableOpt;   /* insert to HTML <table ...> */
    		char	*caption;    /* HTML <caption> */
    		char	**fieldName; /* null terminated array of replacement field names */
    	};

    此函数旨在取代现已过时的 PQprintTuples()。

  • PQprintTuples 打印出所有的元组以及(可选的)属性名到指定的输出流。程序 psql 和 monitor 的输出都使用 PQprintTuples。

    void PQprintTuples(PGresult* res,
                       FILE* fout,      /* output stream */
                       int printAttName,/* print attribute names or not*/
                       int terseOutput, /* delimiter bars or not?*/
                       int width);      /* width of column, variable width if 0*/
  • PQdisplayTuples 打印出所有的元组以及(可选的)属性名到指定的输出流。

    void PQdisplayTuples(
                         PGresult* res,
                         FILE* fout,           /* output stream */
                         int fillAlign,        /* space fill to align columns */
                         const char *fieldSep, /* field separator */
    	                 int printHeader,      /* display headers? */
    		             int quiet);           /* suppress print of row count at end */

    PQdisplayTuples() 本意是取代 PQprintTuples(),而它又被 PQprint() 取代。

  • PQclear 释放与 PGresult 关联的存储。每个查询结果在不再使用时都应妥善释放。不这样做将导致前端应用内存泄漏。

    void PQclear(PQresult *res);

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.