Advertisement
Guest User

rak

a guest
Mar 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libpq-fe.h>
  4.  
  5. void doSQL(PGconn *conn, char *command)
  6. {
  7.     PGresult *result;
  8.     printf("%s\n", command);
  9.     result = PQexec(conn, command);
  10.     printf("status is   : %s\n", PQresSrarus(PQresultStatus(result)));
  11.     printf("#rows aff   : %s\n", PQcmdTuples(result));
  12.     printf("result msg  : %s\n", PQresultErrorMessage(result));
  13.    
  14.     switch(PQresultStatus(result)){
  15.         case PGRES_TUPLES_OK:
  16.             {
  17.                 int n=0, m=0;
  18.                 int nrows = PQntuples(result);
  19.                 int nfields = PQnfields(result);
  20.                 printf("number of rows returned     = %d\n", nrows);
  21.                 printf("number of fields returned   = %d\n", nfields);
  22.                 for(m=0; m < nrows; m++){
  23.                     for(n=0, n < nfields; n++) printf("%s = %s", PQfname(result, n),PQgetvalue(result,m,n));
  24.                     printf("\n");
  25.                 }
  26.             }
  27.     }
  28.     PQclear(result);
  29. }
  30.  
  31. void HTMLprint(PGconn * conn, char *tabela, char *out)
  32. {
  33.     PGresult *result;
  34.     char polec[150];
  35.     sprintf(polec,"SELECT * FROM %s",tabela);
  36.     result = PQexec(conn, polec);
  37.     switch(PQresultStatus(result))
  38.     {
  39.         case PGRES_TUPLES_OK:
  40.         {
  41.             int n = 0, m = 0;
  42.             int nrows   = PQntuples(result); //wiersze
  43.             int nfields = PQnfields(result); //kolumny
  44.             FILE * druk = fopen(out, "w+"); //plik
  45.             fprintf(druk,"<!DOCTYPE html> \n <html lang=\"pl\"> \n <head> \n <meta charset=\"utf-8\"> \n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> \n <title>Dane z tabel</title> \n <style> \n"); //header
  46.             //tutaj style:
  47.             fprintf(druk,"table,td,th%c border: 1px solid black%c \n table %cborder-collapse: collapse%c width: 100%c \n th%c text-align: left%c \n </style></head>\n",123,125,123,59,125,123,125); //znaki specjalne c w ascii
  48.             fprintf(druk,"<body> \n <table> \n");
  49.             fprintf(druk,"<tr>\n");
  50.             for(n=0;n<nfields;n++)
  51.                 fprintf(druk,"<th>%s</th>\n",PQfname(result, n));
  52.             fprintf(druk,"</tr>\n");
  53.             for(m = 0; m < nrows; m++)
  54.             {
  55.                 fprintf(druk,"<tr>\n");
  56.                 for(n = 0; n < nfields; n++)
  57.                     fprintf(druk,"<th>%s</th>\n",PQgetvalue(result,m,n));
  58.                 fprintf(druk,"</tr>\n");
  59.             }
  60.             fprintf(druk,"</table> \n </body> \n </html>");
  61.             fclose(druk);
  62.         }
  63.     }
  64.     printf("\n");
  65.     PQclear(result);
  66. }
  67.  
  68. int main(){
  69.     PGresult *result;
  70.     PGconn *conn = PQconnectdb("host=localhost port=5432 dbname=siusiak user=admin password=admin");
  71.     if(PQstatus(conn) == CONNECTION_OK){
  72.         printf("connection made\n");
  73.         doSQL(conn, "DROP TABLE koledzy");
  74.         doSQL(conn, "CREATE TABLE koledzy(value INTEGER PRIMARY KEY, name VARCHAR(20), surname VARCHAR(20), age VARCHAR(20), gender VARCHAR(20), is_intelligent VARCHAR(20) )");
  75.         doSQL(conn, "INSERT INTO koledzy values(1, 'Damian', 'Seifert', '20', 'carp', 'yes')");
  76.         doSQL(conn, "INSERT INTO koledzy values(2, 'Domniki', 'Banulski', '20', 'golem', 'no')");
  77.         doSQL(conn, "INSERT INTO koledzy values(3 'Ddasdn', 'Sasddasrt', '20', 'sadrp', 'yes')");
  78.         doSQL(conn, "SELECT * FROM koledzy");
  79.         doSQL(conn, "UPDATE koledzy SET name = WHERE value = 3");
  80.         doSQL(conn, "SELECT * FROM koledzy");
  81.         HTMLprint(conn, 'koledzy', 'siusiak.html');
  82.     } else printf("connection failed: %s\n", PQerrorMessage(conn));
  83.     PQfinish(conn);
  84.     return EXIT_SUCCES;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement