Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pgsql/libpq-fe.h>
- int main() {
- PGconn *conn;
- PGresult *res;
- int rec_count;
- int row;
- int col;
- //Creating a connection to database
- conn = PQconnectdb("dbname=riverdata host=ewis.pld.ttu.ee user=student password=iag0582");
- //Check connection status
- if (PQstatus(conn) == CONNECTION_BAD) {
- printf("We were unable to connect to the database\n");
- return 0;
- }
- //Execute query
- //res = PQexec(conn, "SELECT name, length_km FROM river LIMIT 10;");
- //res = PQexec(conn, "SELECT id_river, name, length_km FROM river ORDER BY length_km DESC LIMIT 5;");
- res = PQexec(conn, "SELECT id_river, name, length_km FROM river WHERE id_river=id_mainriver AND length_km >70;");
- //Check query result status
- if (PQresultStatus(res) != PGRES_TUPLES_OK) {
- printf("We did not get any data!\n");
- PQfinish(conn);
- return 0;
- }
- //records count
- rec_count = PQntuples(res);
- printf("We received %d records.\n", rec_count);
- printf("==========================\n");
- //Print received records
- for (row=0; row<rec_count; row++) {
- for (col=0; col<3; col++) {
- printf("%-10s\t", PQgetvalue(res, row, col));
- }
- printf("\n");
- }
- printf("==========================\n");
- //Free PGresult handle
- PQclear(res);
- //Close the connection and free the memory used by PGconn handler
- PQfinish(conn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement