Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <libpq-fe.h>
- void show_table(PGresult *result)
- {
- int n,r,nrows,nfields;
- nrows = PQntuples(result);
- nfields = PQnfields(result);
- for(n=0;n<nfields;n++)
- {
- printf("%s \t|", PQfname(result,n));
- }
- printf("\n");
- for(r=0;r<nrows;r++)
- {
- for(n=0;n<nfields;n++)
- {
- printf(" %s \t |",PQgetvalue(result,r,n));
- }
- printf("\n");
- }
- }
- int main(void)
- {
- PGconn * dbh; //wskaźnik do struktury połączenia
- PGresult *wynik;
- dbh = PQconnectdb(“dbname=wyklad user=bujnows password =
- cccc host=153.19.51.211 port=5432“);
- if (PQstatus(dbh) == CONNECTION_OK) //CONNECTION_BAD
- {
- printf("Udane polaczenie z baza \n");
- // ... tu pracujemy z bazą
- 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;");
- switch(PQresultStatus(wynik))
- {
- case PGRES_TUPLES_OK: //zapytanie zwr dane tutaj sprawdzimy
- show_table(wynik);
- break;
- case PGRES_COMMAND_OK: // nie ma danych
- printf("Zapytanie się powiodlo \n");
- break;
- case PGRES_EMPTY_QUERY:
- printf ("Serwer nie mial nic do roboty, może blad ?\n");
- break;
- case PGRES_NONFATAL_ERROR:
- printf("Blad niekrytyczny, sprobuj ponowic zapytanie \n");
- break;
- case PGRES_FATAL_ERROR:
- default:
- printf("Blad krytyczny \n"); // wyswietlmy status bledu
- printf("%s\n",PQresultErrorMessage(wynik));
- }
- PQclear(wynik); // wyczyscmy wynik o ile jest
- PQfinish(dbh);
- }
- else printf(“Blad polaczenia z baza \n“);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement