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|", PQgetvalue(result, r, 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; //wskaznik do struktury polaczenia
- PGresult *wynik;
- dbh = PQconnectdb("dbname= user = password = host= port= 5432");
- if (PQstatus(dbh) == CONNECTION_OK){ //CONNECTION_BAD
- printf("Udanie polaczenie z baza \n");
- // ... tu pracujemy z baza
- wynik = PQexec(dbh, "SELECT * from klient ");
- switch(PQresultStatus(wynik)){
- case PGRES_TUPLES_OK:
- show_table(wynik);
- break;
- case PGRES_COMMAND_OK: //nie ma danych
- printf("Zapytanie sie powiodlo \n");
- break;
- case PGRES_EMPTY_QUERY:
- printf("Serwer nie mial nic do roboty, moze 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