Se requiere tener instalado cpp y las librerias libpg5
sudo aptitude install libpq5 libpq-dev
Antes que nada debemos tener un servidor de base de datos postgres, login, password
Primero crea una base de datos y luego genera una tabla y algunos registros
create table people ( id integer, firstname varchar(20), lastname varchar(20), phonenumber char(10) ); insert into people (id, firstname, lastname, phonenumber) values (1, 'Fred', 'Flintstone', '5055551234'); insert into people (id, firstname, lastname, phonenumber) values (2, 'Wilma', 'Flintstone', '5055551234'); insert into people (id, firstname, lastname, phonenumber) values (3, 'Barny', 'Rubble', '5055554321');
Este es el codigo c++
#include#include #include #include int main() { PGconn *conn; PGresult *res; int rec_count; int row; int col; conn = PQconnectdb("dbname=pg_cpp_data host=localhost user=empleado password=empleado01"); if (PQstatus(conn) == CONNECTION_BAD) { puts("We were unable to connect to the database"); exit(0); } res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3"); res = PQexec(conn, "select lastname,firstname,phonenumber from people order by id"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { puts("We did not get any data!"); exit(0); } rec_count = PQntuples(res); printf("We received %d records.\n", rec_count); puts("=========================="); for (row=0; row Para compilar el codigo has lo siguiente
g++ -lpq db.cpp -o db
luego ejecurta el programa y te debera salir
hernanr@eros:~/misCodigos/c++/postgresC++$ ./db
We received 3 records.
==========================
Flintstone Fred 5055551234
Flintstone Wilma 5055551234
Rubble Barny 5055559999
==========================