libpq
使用libpq的前端程序必须包括头文件libpq-fe.h并且必须与libpq库链接。
开发流程
解压相应的发布包(如openGauss-*.*.0-***-64bit-Libpq.tar.gz)文件,其中include文件夹下的头文件为所需的头文件,lib文件夹中为所需的libpq库文件。
包含libpq-fe.h头文件:
-
如果要使用制作文件(makefile),向CPPFLAGS、LDFLAGS、LIBS变量中增加如下选项:
CPPFLAGS += -I (头文件所在目录)
LDFLAGS += -L (libpq库所在目录)
LIBS += -lpq
创建表
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=postgres port=42121 host='10.44.133.171' application_name=test connect_timeout=5 sslmode=allow user='test' password='test_1234'";
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32))");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "CREATE command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/* 关闭数据库连接并清理 */
PQfinish(conn);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
{
const char *conninfo;
PGconn *conn;
PGresult *res;
if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=postgres port=42121 host='10.44.133.171' application_name=test connect_timeout=5 sslmode=allow user='test' password='test_1234'";
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "insert into customer_t1 values(25,'li')");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "INSERT command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/* 关闭数据库连接并清理 */
PQfinish(conn);
return 0;
}
SELECT操作
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=postgres port=42121 host='10.44.133.171' application_name=test connect_timeout=5 sslmode=allow user='test' password='test_1234'";
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
exit_nicely(conn);
}
res = PQexec(conn, "update customer_t1 set c_customer_sk = 1000 where c_customer_name = 'li'");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "UPDATE command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/* 关闭数据库连接并清理 */
PQfinish(conn);
return 0;
}
删除操作
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=postgres port=42121 host='10.44.133.171' application_name=test connect_timeout=5 sslmode=allow user='test' password='test_1234'";
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "delete from customer_t1 where c_customer_name = 'li");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DELETE command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/* 关闭数据库连接并清理 */
PQfinish(conn);