Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pgsql/libpq-fe.h>
  3.  
  4. int main() {
  5. PGconn *conn;
  6. PGresult *res;
  7. int rec_count;
  8. int row;
  9. int col;
  10.  
  11. //Creating a connection to database
  12. conn = PQconnectdb("dbname=riverdata host=ewis.pld.ttu.ee user=student password=iag0582");
  13.  
  14. //Check connection status
  15. if (PQstatus(conn) == CONNECTION_BAD) {
  16. printf("We were unable to connect to the database\n");
  17. return 0;
  18. }
  19.  
  20. //Execute query
  21. //res = PQexec(conn, "SELECT name, length_km FROM river LIMIT 10;");
  22. //res = PQexec(conn, "SELECT id_river, name, length_km FROM river ORDER BY length_km DESC LIMIT 5;");
  23. res = PQexec(conn, "SELECT id_river, name, length_km FROM river WHERE id_river=id_mainriver AND length_km >70;");
  24.  
  25. //Check query result status
  26. if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  27. printf("We did not get any data!\n");
  28. PQfinish(conn);
  29. return 0;
  30. }
  31.  
  32. //records count
  33. rec_count = PQntuples(res);
  34.  
  35. printf("We received %d records.\n", rec_count);
  36.  
  37. printf("==========================\n");
  38.  
  39. //Print received records
  40. for (row=0; row<rec_count; row++) {
  41. for (col=0; col<3; col++) {
  42. printf("%-10s\t", PQgetvalue(res, row, col));
  43. }
  44. printf("\n");
  45. }
  46.  
  47. printf("==========================\n");
  48.  
  49. //Free PGresult handle
  50. PQclear(res);
  51.  
  52. //Close the connection and free the memory used by PGconn handler
  53. PQfinish(conn);
  54.  
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement