Commit ecd61211 by rakein

29mar17

parent 4df57c30
#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 FROM station WHERE id_river = (SELECT id_river FROM river WHERE length_km >= 70 AND id_river = id_mainriver;");**/
res = PQexec(conn, "SELECT station.name,river.length_km, river.name FROM station INNER JOIN river ON station.id_river = river.id_river WHERE river.length_km >= 70 AND river.id_river = river.id_mainriver ORDER BY length_km;");
//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;
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment