Advertisement
Guest User

rakmocno

a guest
Mar 22nd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <libpq-fe.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #define LOW 25
  7. #define HIGH 225
  8.  
  9. void doSQL(PGconn *conn, char *command)
  10. {
  11.     PGresult *result;
  12.     printf("%s\n", command);
  13.     result = PQexec(conn, command);
  14.     printf("status is   : %s\n", PQresStatus(PQresultStatus(result)));
  15.     printf("#rows aff   : %s\n", PQcmdTuples(result));
  16.     printf("result msg  : %s\n", PQresultErrorMessage(result));
  17.    
  18.     switch(PQresultStatus(result)){
  19.         case PGRES_TUPLES_OK:
  20.             {
  21.                 int n=0, m=0;
  22.                 int nrows = PQntuples(result);
  23.                 int nfields = PQnfields(result);
  24.                 printf("number of rows returned     = %d\n", nrows);
  25.                 printf("number of fields returned   = %d\n", nfields);
  26.                 for(n=0; n < nfields; n++){
  27.                         printf("%7s ", PQfname(result, n));
  28.                     }
  29.                 printf("\n");
  30.                 for(m=0; m < nrows; m++){
  31.  
  32.                     for(n=0; n < nfields; n++){
  33.                         printf("%7s ", PQgetvalue(result,m,n));
  34.                     }
  35.                     printf("\n");
  36.                 }
  37.             }
  38.     }
  39.     PQclear(result);
  40. }
  41.  
  42. void HTMLprint(PGconn * conn, char *tabela, char *out)
  43. {
  44.     PGresult *result;
  45.     char stmt[150];
  46.     sprintf(stmt,"SELECT * FROM %s",tabela);
  47.     result = PQexec(conn, stmt);
  48.     switch(PQresultStatus(result))
  49.     {
  50.         case PGRES_TUPLES_OK:
  51.         {
  52.             int n = 0, m = 0;
  53.             int nrows   = PQntuples(result); //wiersze
  54.             int nfields = PQnfields(result); //kolumny
  55.             FILE * druk = fopen(out, "w+"); //plik
  56.             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>Wynik</title> \n <style> \n"); //header
  57.             //tutaj style:
  58.             fprintf(druk,"table,td,th{ border: 5px solid red; background:pink; border-radius: 5px;} \n table {border-collapse: collapse: width: 100} \n th{ text-align: left; font-family: Comic Sans MS} \n body{background-color: teal} </style></head>\n");
  59.             fprintf(druk,"<body> \n <table> \n");
  60.             fprintf(druk,"<tr>\n");
  61.             for(n=0;n<nfields;n++)
  62.                 fprintf(druk,"<th>%s</th>\n",PQfname(result, n));
  63.             fprintf(druk,"</tr>\n");
  64.             for(m = 0; m < nrows; m++)
  65.             {
  66.                 fprintf(druk,"<tr>\n");
  67.                 for(n = 0; n < nfields; n++)
  68.                     fprintf(druk,"<th>%s</th>\n",PQgetvalue(result,m,n));
  69.                 fprintf(druk,"</tr>\n");
  70.             }
  71.             fprintf(druk,"</table> \n </body> \n </html>");
  72.             fclose(druk);
  73.         }
  74.     }
  75.     printf("\n");
  76.     PQclear(result);
  77. }
  78.  
  79. int main(int argc, char *argv[]){
  80.    
  81.     int i=0;
  82.     int rows=0, columns=0;
  83.    
  84.     char file[LOW], fileName[LOW], fileExt[LOW]=".csv";
  85.     char *table[LOW];
  86.     char line[LOW], sqlCommand[HIGH], columnNames[HIGH][HIGH];
  87.    
  88.     FILE *fileContents;
  89.    
  90.     printf("Podaj nazwe pliku .csv:\n");
  91.     scanf("%s", &file);
  92.    
  93.     strcpy(fileName,file);
  94.    
  95.    
  96.     printf("%s",file);
  97.    
  98.     PGresult *result;
  99.     PGconn *conn = PQconnectdb("host=localhost port=5432 dbname=nazwa user=pstankiewicz password=mokrawydra");
  100.     if(PQstatus(conn) == CONNECTION_OK){
  101.         printf("connection made\n");
  102.        
  103.         strcat(file,".csv");
  104.         if((fileContents = fopen(file, "r")) == NULL){
  105.             printf("Nie znaleziono pliku");
  106.             return EXIT_SUCCESS;
  107.         }
  108.         fscanf(fileContents,"%s",line);
  109.     //  FILE *fileContents = fopen("koledzy.csv", "r");
  110.        
  111.         table[i] = strtok(line,";");
  112.    
  113.         while(table[i]!=NULL)
  114.         {
  115.             table[++i] = strtok(NULL,";");
  116.             columns++;
  117.         }  
  118.        
  119.        
  120.         for(i=0;i<columns;i++)
  121.             strcpy(columnNames[i],table[i]);
  122.            
  123.         for(i=1;table[i]!=NULL;i++){
  124.             printf("\n>>>>>>>>>>>>>>%s\n",table[i]);
  125.         //  sprintf(sqlCommand,"ALTER TABLE %s ADD %s VARCHAR(20)", fileName, table[i]);
  126.         //  doSQL(conn,sqlCommand);
  127.         }
  128.        
  129.         sprintf(sqlCommand,"DROP TABLE %s", fileName);
  130.         doSQL(conn,sqlCommand);
  131.        
  132.         sprintf(sqlCommand,"CREATE TABLE %s(%s INTEGER UNIQUE)", fileName, table[0]);
  133.         doSQL(conn,sqlCommand);
  134.        
  135.         for(i=1;table[i]!=NULL;i++){
  136.             printf("\n>>>>>>>>>>>>>>%s\n",table[i]);
  137.         //  sprintf(sqlCommand,"ALTER TABLE %s ADD %s VARCHAR(20)", fileName, table[i]);
  138.         //  doSQL(conn,sqlCommand);
  139.         }
  140.        
  141.         for(i=1;table[i]!=NULL;i++){
  142.             printf("\n\n>>>>>>>>>>>>>>%s\n\n",table[i]);
  143.             sprintf(sqlCommand,"ALTER TABLE %s ADD %s VARCHAR(20)", fileName, table[i]);
  144.             doSQL(conn,sqlCommand);
  145.         }
  146.        
  147.         while(fscanf(fileContents,"%s",line) == 1){
  148.             i=0;
  149.             table[i] = strtok(line,";");
  150.            
  151.             for(;i<columns;)
  152.                 table[++i] = strtok(NULL,";");
  153.            
  154.             for(i=0;i<columns;i++) // extend column size if needed
  155.             {
  156.                 if(strlen(table[i])>20)
  157.                 {
  158.                     sprintf(sqlCommand,"ALTER TABLE %s ALTER COLUMN %s TYPE VARCHAR(%d)", fileName, columnNames[i], (int)strlen(table[i]));
  159.                     doSQL(conn, sqlCommand);
  160.                 }
  161.             }
  162.        
  163.             sprintf(sqlCommand,"INSERT INTO %s VALUES(%s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", fileName, table[0], table[1], table[2], table[3], table[4], table[5], table[6], table[7], table[8]);
  164.             doSQL(conn, sqlCommand);
  165.         }
  166.        
  167.         HTMLprint(conn, argv[1], argv[2]);
  168.        
  169.     } else printf("connection failed: %s\n", PQerrorMessage(conn));
  170.     PQfinish(conn);
  171.     return EXIT_SUCCESS;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement