Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <libpq-fe.h>
  3.  
  4. void show_table(PGresult *result){
  5. int n,r,nrows,nfields;
  6. nrows = PQntuples(result);
  7. nfields = PQnfields(result);
  8. for (n=0; n<nfields; n++){
  9. printf("%s \t|", PQgetvalue(result, r, n));
  10. }
  11. printf("\n");
  12. for (r=0; r<nrows; r++){
  13. for (n=0; n<nfields; n++){
  14. printf(" %s \t |",PQgetvalue(result, r, n));
  15. }
  16. }
  17. printf("\n");
  18. }
  19. int main(void){
  20. PGconn * dbh; //wskaznik do struktury polaczenia
  21. PGresult *wynik;
  22. dbh = PQconnectdb("dbname= user = password = host= port= 5432");
  23. if (PQstatus(dbh) == CONNECTION_OK){ //CONNECTION_BAD
  24. printf("Udanie polaczenie z baza \n");
  25. // ... tu pracujemy z baza
  26. wynik = PQexec(dbh, "SELECT * from klient ");
  27. switch(PQresultStatus(wynik)){
  28. case PGRES_TUPLES_OK:
  29. show_table(wynik);
  30. break;
  31. case PGRES_COMMAND_OK: //nie ma danych
  32. printf("Zapytanie sie powiodlo \n");
  33. break;
  34. case PGRES_EMPTY_QUERY:
  35. printf("Serwer nie mial nic do roboty, moze blad? \n");
  36. break;
  37. case PGRES_NONFATAL_ERROR:
  38. printf("Blad niekrytyczny, sprobuj ponowic zapytanie \n");
  39. break;
  40. case PGRES_FATAL_ERROR:
  41. default:
  42. printf("Blad krytyczny \n"); //wyswietlmy status bledu
  43. printf("%s\n",PQresultErrorMessage(wynik));
  44. }
  45. PQclear(wynik); //wyczyscmy wynik o ile jest
  46.  
  47. PQfinish(dbh);
  48. }
  49. else printf("Blad polaczenia z baza \n");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement