Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1.  #include <postgresql/libpq-fe.h>
  2.  #include <string.h>
  3. #include <stdio.h>
  4.  
  5. struct Insumo{
  6.     char *nombre;
  7. };
  8. struct Insumo insumos[50];
  9.  
  10. int main(){
  11.  
  12.     PGconn          *conn;
  13.     PGresult        *res;
  14.     int             rec_count;
  15.     int             row;
  16.  
  17.          conn = PQconnectdb("dbname=SistemaProduccion host=localhost user=postgres password=andyjosue");
  18.  
  19.          if (PQstatus(conn) == CONNECTION_BAD) {
  20.                  printf("We were unable to connect to the database");
  21.                  
  22.          }
  23.  
  24.          
  25.  
  26.         res = PQexec(conn,
  27.                  "select * from Insumos");
  28.  
  29.          if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  30.                  printf("We did not get any data!");
  31.                
  32.          }
  33.  
  34.          rec_count = PQntuples(res);
  35.  
  36.          printf("We received %d records.\n", rec_count);
  37.          printf("==========================\n");
  38.  
  39.          for (row=0; row<rec_count; row++) {
  40.                
  41.                         // printf("%s\t", PQgetvalue(res, row, 1));
  42.                           insumos[row].nombre = PQgetvalue(res, row, 1);
  43.                          //insumos[row]->nombre = PQgetvalue(res, row, 1);
  44.                  
  45.                      printf("\n");
  46.          }
  47.         for(int k=0;k<rec_count;k++){
  48.             printf("%s \n",insumos[0].nombre);
  49.         }
  50.        
  51.          PQclear(res);
  52.  
  53.          PQfinish(conn);
  54.  
  55.          return 0;
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement