[原]C语言连接postgres的实例 - libpq使用
Posted by bianbian on 2006-12-20 15:28
看过长篇文档后,啥废话也不说了,来个实例总结:
- char *sql = "SELECT userid,password FROM users WHERE ....";
- PGconn *conn = PQconnectdb("host=localhost user=postgres password= dbname=db port=5432");
- if (PQstatus(conn) == CONNECTION_OK)
- {
- PQsetClientEncoding(conn, "UNICODE");
- PGresult *res = PQexec(conn, sql);
- if (res != null)
- {
- int status = PQresultStatus(res);
- if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)
- {
- char *res_userid = PQgetvalue(res, 0, 0);
- char *res_password = PQgetvalue(res, 0, 1);
- ....
- }
- PQclear(res);
- }
- }
- PQfinish(conn);
遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道
December 20th, 2006 at 03:47:06
好像pgsql的文档里面的example就有这样的例子……
December 20th, 2006 at 04:40:09
-_-! 那我错了…. 我其实就看了第一页文档….