免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4594 | 回复: 2
打印 上一主题 下一主题

c/c++连接postgresql [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 19:16 |只看该作者 |倒序浏览
c/c++怎样连接本机postgresql数据库,还有在表中读取和插入数据是什么格式的?

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
2 [报告]
发表于 2011-12-22 21:54 |只看该作者
参考联机帮助的 libpq 接口,这是pg自己的通信接口
贴一个8.2的例子:
  1. /*
  2. * testlibpq.c
  3. *
  4. *      Test C 版本的 libpq,PostgreSQL 前端库
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "libpq-fe.h"

  9. static void
  10. exit_nicely(PGconn *conn)
  11. {
  12.     PQfinish(conn);
  13.     exit(1);
  14. }

  15. int
  16. main(int argc, char **argv)
  17. {
  18.     const char *conninfo;
  19.     PGconn     *conn;
  20.     PGresult   *res;
  21.     int         nFields;
  22.     int         i,
  23.                 j;

  24.     /*
  25.      * 如果用户在命令行上提供了一个参数,则拿它当作 conninfo 字串使用;
  26.      * 否则缺省为 dbname=postgres 并且使用环境变量或者所有其它连接参数
  27.      * 都使用缺省值。
  28.      */
  29.     if (argc > 1)
  30.         conninfo = argv[1];
  31.     else
  32.         conninfo = "dbname = postgres";

  33.     /* Make a connection to the database */
  34.     conn = PQconnectdb(conninfo);

  35.     /* 检查后端连接成功建立 */
  36.     if (PQstatus(conn) != CONNECTION_OK)
  37.     {
  38.         fprintf(stderr, "Connection to database failed: %s",
  39.                 PQerrorMessage(conn));
  40.         exit_nicely(conn);
  41.     }

  42.     /*
  43.      * 我们的测试实例涉及游标的使用,这个时候我们必须使用事务块
  44.      * 我们可以把全部事情放在一个  "select * from pg_database"
  45.      * PQexec() 里,不过那样太简单了,不是个好例子。
  46.      */

  47.     /* 开始一个事务块 */
  48.     res = PQexec(conn, "BEGIN");
  49.     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  50.     {
  51.         fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
  52.         PQclear(res);
  53.         exit_nicely(conn);
  54.     }

  55.     /*
  56.      * 应该在结果不需要的时候 PQclear PGresult,以避免内存泄漏
  57.      */
  58.     PQclear(res);

  59.     /*
  60.      * 从系统表 pg_database 里抓取数据
  61.      */
  62.     res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
  63.     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  64.     {
  65.         fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
  66.         PQclear(res);
  67.         exit_nicely(conn);
  68.     }
  69.     PQclear(res);

  70.     res = PQexec(conn, "FETCH ALL in myportal");
  71.     if (PQresultStatus(res) != PGRES_TUPLES_OK)
  72.     {
  73.         fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
  74.         PQclear(res);
  75.         exit_nicely(conn);
  76.     }

  77.     /* 首先,打印属性名称 */
  78.     nFields = PQnfields(res);
  79.     for (i = 0; i < nFields; i++)
  80.         printf("%-15s", PQfname(res, i));
  81.     printf("\n\n");

  82.     /* 然后打印行 */
  83.     for (i = 0; i < PQntuples(res); i++)
  84.     {
  85.         for (j = 0; j < nFields; j++)
  86.             printf("%-15s", PQgetvalue(res, i, j));
  87.         printf("\n");
  88.     }

  89.     PQclear(res);

  90.     /* 关闭游标 ... 我们不用检查错误 ... */
  91.     res = PQexec(conn, "CLOSE myportal");
  92.     PQclear(res);

  93.     /* 结束事务 */
  94.     res = PQexec(conn, "END");
  95.     PQclear(res);

  96.     /* 关闭数据库连接并清理 */
  97.     PQfinish(conn);

  98.     return 0;
  99. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-01-30 10:59 |只看该作者
通过PG的标准头文件看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP