Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <libpq-fe.h>
  3. void show_table(PGresult *result)
  4. {
  5. int n,r,nrows,nfields;
  6. nrows = PQntuples(result);
  7. nfields = PQnfields(result);
  8. for(n=0;n<nfields;n++)
  9. {
  10. printf("%s \t|", PQfname(result,n));
  11. }
  12. printf("\n");
  13. for(r=0;r<nrows;r++)
  14. {
  15. for(n=0;n<nfields;n++)
  16. {
  17. printf(" %s \t |",PQgetvalue(result,r,n));
  18. }
  19. printf("\n");
  20. }
  21. }
  22. int main(void)
  23.  
  24. {
  25. PGconn * dbh; //wskaźnik do struktury połączenia
  26. PGresult *wynik;
  27. dbh = PQconnectdb(“dbname=wyklad user=bujnows password =
  28. cccc host=153.19.51.211 port=5432);
  29. if (PQstatus(dbh) == CONNECTION_OK) //CONNECTION_BAD
  30. {
  31. printf("Udane polaczenie z baza \n");
  32. // ... tu pracujemy z bazą
  33. wynik = PQexec(dbh,"select count(*) as liczba_klientow, (select count(*) from plyta) as liczba_plyt,(select count(*) from gatunek)as liczba_gatunkow,(select count(*) from wypozyczenie)as liczba_wypozyczen from klient;");
  34. switch(PQresultStatus(wynik))
  35. {
  36. case PGRES_TUPLES_OK: //zapytanie zwr dane tutaj sprawdzimy
  37. show_table(wynik);
  38. break;
  39. case PGRES_COMMAND_OK: // nie ma danych
  40. printf("Zapytanie się powiodlo \n");
  41. break;
  42. case PGRES_EMPTY_QUERY:
  43. printf ("Serwer nie mial nic do roboty, może blad ?\n");
  44. break;
  45. case PGRES_NONFATAL_ERROR:
  46. printf("Blad niekrytyczny, sprobuj ponowic zapytanie \n");
  47. break;
  48. case PGRES_FATAL_ERROR:
  49. default:
  50. printf("Blad krytyczny \n"); // wyswietlmy status bledu
  51. printf("%s\n",PQresultErrorMessage(wynik));
  52. }
  53. PQclear(wynik); // wyczyscmy wynik o ile jest
  54. PQfinish(dbh);
  55. }
  56. else printf(“Blad polaczenia z baza \n“);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement