加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C语言中使用libpq访问Postgresql数据库

发布时间:2020-12-13 17:54:32 所属栏目:百科 来源:网络整理
导读:一,如何启动postgresql. 1,成为管理员 $ su postgres 2,与服务器连接 $ psql DbName 3,创建数据库 $ createdb Dbname $ dropdb DBname 二,编译 1,包含libpq-fe.h头文件。 2,编译时写入库所有目录。 3,连接pq程序库。 $ gcc program.c -o program -I /usr

一,如何启动postgresql. 1,成为管理员 $ su postgres 2,与服务器连接 $ psql <DbName> 3,创建数据库 $ createdb <Dbname> $ dropdb <DBname> 二,编译 1,包含libpq-fe.h头文件。 2,编译时写入库所有目录。 3,连接pq程序库。 $ gcc program.c -o program -I /usr/include/postgres -lpq 三,函数: 1,连接数据库 PGconn *PQconnectdb(const char *conninfo); 对于conninfo的值,形式如: conninfo= "host=localhost port=5432 dbname=my' 'db user=noyear password=12' '345 "; 表示连接本地机,端口5432,数据库为"my db",使用用户名 noyear 密码为“12 345“;还有两个选项未列出来:options表示跟踪选项,tty表示后端处理器的高度 输出文件或终端。 如果失败,则返回 NULL,这时可以通过 PQstatus函数检查连接。 ConnStatusType PQstatus (PGconn *conn); 返回一个意义明确的枚举类型的值:CONNECTION_OK或 CONNECTION_BAD. 连接出了问题,可以用它获得一条有用出错信息 char *PQerrorMessage(PGconn *conn); 2,执行SQL?句 共有1个执行函数和3个检查?果和读取出错信息的函数; 执行SQL函数: PGresult *PQexec(PGconn *conn,const char *sql_string); 检查返回 : ExecStatusType *PQresultStatus(PGresult *result); ExecStatusType是枚举类型,取值: PGRES_EMPTY_QUERY 不做任何事情 PGRES_COMMAND_OK 命令执行成功,但由于不是SELECT 命令,所惟不返回 数 据。 PGRES_TUPLES_OK 命令执行成功,并且可能返回一些数据。 PGRES_COPY_OUT 复制到外部文件 PGRES_COPY_IN 从外部文件复制 PGRES_BAD_RESPONSE 意外事件发生 PGRES_NONFATAL_ERROR 非致命错误发生 PGRES_FATAL_ERROR 致命错误发生 查看文本的出错消息: const char * PQresultErrorMessage(PGresult *result); 3,获得?果 确定命令所影响的行数: const char *PQcmdTuples(PGresult *result); 返回一串以NULL结尾的数字字符串,是字符格式而不是整数格式。 在找到?果对象后,通知库释放所分配的内存,防止内存泄漏: void PQclear(PQresult *result); 调试时用的函数-把状态的枚举类型转换成串描述符: const char *PQresStatus(ExecStatusType status); 将查询结果发送给文件流 void PQprint(FILE *stream,PGresult *result,PQprintOpt *options); 选项结构体PQprintOpt: typedef struct _PQprintOpt { pgbool header; //print output field headings and row count pgbool align; //fill align the fields pgbool standard; //old brain dead format pgbool html3; //output html tables pgbool expanded; //expand tables pgbool pager; //use paper 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 } PQprintOpt; 游标。。。。遍历 ?果数据。 伪码: BEGIN A TRANSACTION DECLARE CURSOR mycursor FOR SELECT-statement OPEN mycursor DO { FETCH some data form mycursor Process the row(s) retrieved } WHILE the FETCH command found data CLOSE mycursor COMMIT WORK 数据返回中我们可以从PGresult中取得列的信息: 取得列数: int PQnfields(PGresult *result); 取得每一列的名称(第一列处于索引0): char *PQfname(PGresult *result,int field_index); 取得数据的大小,返回的是PostgreSQL的内部空间,VARCHAR返回 -1; int PQfsize(PGresult *result,int field_index); 访问检索数据 取得数据时,用PQgetlength来获得返回 数据信息的长度 int PQgetlength(PGresult *result,int tuple_number,int field_index); 取得数据的字符串描述信息: char *PQgetvalue(PGresult *result,int field_index); 检测返回是否在数据库中以NULL(表示未知)值: int PQgetisnull(PGresult *result,int field_index); 4,终止连接 正常的或非正常的都要调用它,因为它还要释放资源: void PQfinish(PGconn *conn);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读