$ sudo apt-get install postgresql
$ sudo -u postgres psql template1
# \password postgres
# create database testdb;
At this point, we’ve the PostgreSQL database created and we can now writing some C++ code to establish connection to this database. We will start the coding part in our cpp file now. I called the cpp file – “PSQLTest.cpp”. Make sure that you include the following header accordingly. libpq-fe.h must be included.
1 2 3 4 | #include <iostream> #include "libpq-fe.h" using namespace std; |
1 2 3 4 5 6 | /* Close connection to database */ void CloseConn(PGconn *conn) { PQfinish(conn); getchar(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* Establish connection to database */ PGconn *ConnectDB() { PGconn *conn = NULL; // Make a connection to the database conn = PQconnectdb("user=postgres password=test123 dbname=testdb hostaddr=127.0.0.1 port=5432"); // Check to see that the backend connection was successfully made if (PQstatus(conn) != CONNECTION_OK) { cout << "Connection to database failed.\n"; CloseConn(conn); } cout << "Connection to database - OK\n"; return conn; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Create employee table */ void CreateEmployeeTable(PGconn *conn) { // Execute with sql statement PGresult *res = PQexec(conn, "CREATE TABLE employee (Fname char(30), Lname char(30))"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "Create employee table failed\n"; PQclear(res); CloseConn(conn); } cout << "Create employee table - OK\n"; // Clear result PQclear(res); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* Append SQL statement and insert record into employee table */ void InsertEmployeeRec(PGconn *conn, const char * fname, const char * lname) { // Append the SQL statment std::string sSQL; sSQL.append("INSERT INTO employee VALUES ('"); sSQL.append(fname); sSQL.append("', '"); sSQL.append(lname); sSQL.append("')"); // Execute with sql statement PGresult *res = PQexec(conn, sSQL.c_str()); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "Insert employee record failed\n"; PQclear(res); CloseConn(conn); } cout << "Insert employee record - OK\n"; // Clear result PQclear(res); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /* Fetch employee record and display it on screen */ void FetchEmployeeRec(PGconn *conn) { // Will hold the number of field in employee table int nFields; // Start a transaction block PGresult *res = PQexec(conn, "BEGIN"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "BEGIN command failed\n"; PQclear(res); CloseConn(conn); } // Clear result PQclear(res); // Fetch rows from employee table res = PQexec(conn, "DECLARE emprec CURSOR FOR select * from employee"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "DECLARE CURSOR failed\n"; PQclear(res); CloseConn(conn); } // Clear result PQclear(res); res = PQexec(conn, "FETCH ALL in emprec"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { cout << "FETCH ALL failed\n"; PQclear(res); CloseConn(conn); } // Get the field name nFields = PQnfields(res); // Prepare the header with employee table field name cout << "\nFetch employee record:"; cout << "\n********************************************************************\n"; for (int i = 0; i < nFields; i++) printf("%-30s", PQfname(res, i)); cout << "\n********************************************************************\n"; // Next, print out the employee record for each row for (int i = 0; i < PQntuples(res); i++) { for (int j = 0; j < nFields; j++) printf("%-30s", PQgetvalue(res, i, j)); printf("\n"); } PQclear(res); // Close the emprec res = PQexec(conn, "CLOSE emprec"); PQclear(res); // End the transaction res = PQexec(conn, "END"); // Clear result PQclear(res); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Erase all record in employee table */ void RemoveAllEmployeeRec(PGconn *conn) { // Execute with sql statement PGresult *res = PQexec(conn, "DELETE FROM employee"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "Delete employees record failed.\n"; PQclear(res); CloseConn(conn); } cout << "\nDelete employees record - OK\n"; // Clear result PQclear(res); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Drop employee table from the database*/ void DropEmployeeTable(PGconn *conn) { // Execute with sql statement PGresult *res = PQexec(conn, "DROP TABLE employee"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { cout << "Drop employee table failed.\n"; PQclear(res); CloseConn(conn); } cout << "Drop employee table - OK\n"; // Clear result PQclear(res); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | int main() { PGconn *conn = NULL; conn = ConnectDB(); if (conn != NULL) { CreateEmployeeTable(conn); InsertEmployeeRec(conn, "Mario", "Hewardt"); InsertEmployeeRec(conn, "Daniel", "Pravat"); FetchEmployeeRec(conn); cout << "\nPress ENTER to remove all records & table.....\n"; getchar(); RemoveAllEmployeeRec(conn); DropEmployeeTable(conn); CloseConn(conn); } return 0; } |
$ g++ PSQLTest.cpp -I /usr/include/postgresql/include -l pq -o PSQLTest
$ ./PSQLTest
$ ./PSQLTest
If you try to break the C++ program before it remove and drop the employee table. Enter to psql command console and you should be able to read you table data as the following screenshot.
Download sample source code: PSQLTest.zip
I am using UBUNTU 11.10 and I need to install ( libpq-dev ) also to compile the program
sudo apt-get install libpq-dev
I always get this error:
In file included from /usr/lib/gcc/i686-linux-gnu/4.7/include/libpq-fe.h:29:0,
from main.cpp:2:
/usr/lib/gcc/i686-linux-gnu/4.7/include/postgres_ext.h:43:9: error: ‘PG_INT64_TYPE’ does not name a type
In file included from main.cpp:2:0:
/usr/lib/gcc/i686-linux-gnu/4.7/include/libpq-fe.h:542:8: error: ‘pg_int64’ does not name a type
/usr/lib/gcc/i686-linux-gnu/4.7/include/libpq-fe.h:546:8: error: ‘pg_int64’ does not name a type
/usr/lib/gcc/i686-linux-gnu/4.7/include/libpq-fe.h:548:48: error: ‘pg_int64’ has not been declared
Any thought would be apriciated.
mind to share what’s the linux distro that you are using?
Xubuntu 12.10
this is the result of “uname -v”:
#37-Ubuntu SMP Thu Feb 7 05:32:22 UTC 2013
Linux marc-desktop 3.5.0-24-generic #37-Ubuntu SMP Thu Feb 7 05:32:22 UTC 2013 i686 i686 i686 GNU/Linux
it seems like __int64 is not being recognized by your system.
try to use long long instead.
e.g
#define pg_int64 long long