c/c++连接postgresql之后的基本使用函数
一、测试是否连接成功
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
void do_exit(PGconn *conn) {
PQfinish(conn);
exit(1);
}
int main() {
PGconn *conn = PQconnectdb("user=postgres password=123 dbname=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
do_exit(conn);
}
char *user = PQuser(conn);
char *db_name = PQdb(conn);
char *pswd = PQpass(conn);
printf("User: %s\n", user);
printf("Database name: %s\n", db_name);
printf("Password: %s\n", pswd);
PQfinish(conn);
return 0;
}
二、通过c向数据库添加表及数据
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
void do_exit(PGconn *conn, PGresult *res) {
fprintf(stderr, "%s\n", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
}
int main() {
PGconn *conn = PQconnectdb("user=postgres password=123 dbname=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
PGresult *res = PQexec(conn, "DROP TABLE IF EXISTS Cars");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "CREATE TABLE Cars(Id INTEGER PRIMARY KEY," \
"Name VARCHAR(20), Price INT)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(1,'Audi',52642)");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
do_exit(conn, res);
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(2,'Mercedes',57127)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(3,'Skoda',9000)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(4,'Volvo',29000)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(5,'Bentley',350000)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(6,'Citroen',21000)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(7,'Hummer',41400)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
res = PQexec(conn, "INSERT INTO Cars VALUES(8,'Volkswagen',21600)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
PQfinish(conn);
return 0;
}
三、读取数据库中数据
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
void do_exit(PGconn *conn) {
PQfinish(conn);
exit(1);
}
int main() {
PGconn *conn = PQconnectdb("user=postgres password=123 dbname=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
do_exit(conn);
}
PGresult *res = PQexec(conn, "SELECT * FROM Cars LIMIT 5");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printf("No data retrieved\n");
PQclear(res);
do_exit(conn);
}
int rows = PQntuples(res);
for(int i=0; i<rows; i++) {
printf("%s %s %s\n", PQgetvalue(res, i, 0),
PQgetvalue(res, i, 1), PQgetvalue(res, i, 2));
}
PQclear(res);
PQfinish(conn);
return 0;
}
转载自:https://blog.csdn.net/zsc201825/article/details/83379100