Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. /*
  2.  * Program tworzy nowa tabele w bazie danych z nazwa pliku podanego jako argument.
  3.  * Autor: Przemyslaw Foltynski
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <libpq-fe.h>
  12.  
  13. PGconn *connection;
  14.  
  15. void doSQL(PGconn *conn, char *command)
  16. {
  17.   PGresult *result;
  18.  
  19.   printf("%s\n", command);
  20.  
  21.   result = PQexec(conn, command);
  22.   printf("status is     : %s\n", PQresStatus(PQresultStatus(result)));
  23.   printf("#rows affected: %s\n", PQcmdTuples(result));
  24.   printf("result message: %s\n", PQresultErrorMessage(result));
  25.  
  26.   switch(PQresultStatus(result)) {
  27.   case PGRES_TUPLES_OK:
  28.     {
  29.       int n = 0, m = 0;
  30.       int nrows   = PQntuples(result);
  31.       int nfields = PQnfields(result);
  32.       printf("number of rows returned   = %d\n", nrows);
  33.       printf("number of fields returned = %d\n", nfields);
  34.       for(m = 0; m < nrows; m++) {
  35.     for(n = 0; n < nfields; n++)
  36.       printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
  37.     printf("\n");
  38.       }
  39.     }
  40.   }
  41.   PQclear(result);
  42. }
  43.  
  44.  
  45. /*
  46.  *  Funkcja sluzy do budowania polecen SQL skladajacych sie z wielu lancuchow znakow.
  47.  *      @param dest Tablica do ktorej zostanie wpisane wygenerowane polecenie.
  48.  *      @param num  Liczba dodatkowych argumentow (nie liczac dest i num).
  49.  */
  50. void cmdBuilder(char dest[], int num, ...)
  51. {
  52.     int i;
  53.     dest[0] = '\0';
  54.     va_list args;
  55.     va_start(args, num);
  56.     for (i = 0; i < num; i++)
  57.     {
  58.         strcpy(dest + strlen(dest), va_arg(args, char*));
  59.     }
  60.     va_end(args);
  61. }
  62.  
  63. /*
  64.  *  Funkcja dodaje nowy wpis do bazy danych na podstawie lini zczytanej z pliku.
  65.  *      @param name     Nazwa tabeli do ktorej zostanie dodany wpis.
  66.  *      @param entry    Wiersz z pliku zawierajacy dane do dodania.
  67.  */
  68. void addEntry(char* name, char* entry)
  69. {
  70.     int i = 0;
  71.     char colName[20];
  72.     char** colData = (char**) malloc(sizeof(char*)*10);
  73.     for (i = 0; i < 10; i++)
  74.         colData[i] = (char*) malloc(sizeof(char)*20);
  75.     char cmd[400];
  76.  
  77.     extractDataFromLine(colData, entry);
  78.  
  79.     cmdBuilder(cmd, 23, "INSERT INTO ", name, " values(", colData[0], ", '", colData[1], "', '", colData[2], "', '", colData[3], "', '", colData[4], "', '", colData[5], "', '", colData[6], "', '", colData[7], "', '", colData[8], "', '", colData[9], "')");
  80.     doSQL(connection, cmd);
  81. }
  82.  
  83. /*
  84.  * Na podstawie wiersza z pliku wypelnia podana tabele polami oddzielonymi znakiem ';'
  85.  */
  86. void extractDataFromLine(char* dest[], char* source)
  87. {
  88.     int i = 0;
  89.     char* pch;
  90.     pch = strtok(source, ";");
  91.     while (pch != NULL)
  92.     {
  93.         strcpy(dest[i], pch);
  94.         pch = strtok(NULL, ";");
  95.         i++;
  96.     }
  97. }
  98.  
  99. /*
  100.  *  Tworzy nowa tabele.
  101.  *      @param name     Nazwa tabeli (jesli tabela o podanej nazwie juz istnieje to najpierw zostanie ona usunieta).
  102.  *      @param entry    Wiersz z pliku na podstawie ktorego zostanie stworzona tabela.
  103.  */
  104. void buildTable(char* name, char* entry)
  105. {
  106.     int i = 0;
  107.     char colName[20];
  108.     char** colData = (char**) malloc(sizeof(char*)*10);
  109.     for (i = 0; i < 10; i++)
  110.         colData[i] = (char*) malloc(sizeof(char)*20);
  111.     char cmd[400];
  112.  
  113.     extractDataFromLine(colData, entry);
  114.  
  115.    
  116.     cmdBuilder(cmd, 2, "DROP TABLE ", name);
  117.     doSQL(connection, cmd);
  118.     cmdBuilder(cmd, 23, "CREATE TABLE ", name, "( ", colData[0], " VARCHAR(30) PRIMARY KEY, ", colData[1], " VARCHAR(30), ", colData[2], " VARCHAR(30), ", colData[3], " VARCHAR(30), ", colData[4], " VARCHAR(30), ", colData[5], " VARCHAR(30), ", colData[6], " VARCHAR(30), ", colData[7], " VARCHAR(30), ", colData[8], " VARCHAR(30), ", colData[9], " VARCHAR(30))");
  119.     doSQL(connection, cmd);
  120. }
  121.  
  122. int main(int argc, char* argv[])
  123. {
  124.     char line[150];
  125.     char *lineFirstChar;
  126.     char tableName[20];
  127.     char fileName[20];
  128.     char xyz[14] = { 99, 104, 97, 115, 108, 111, 119, 121, 103, 97, 115, 108, 111, '\0'};
  129.     char connString[100] = "host=localhost port=5432 dbname=pfoltynski user=pfoltynski password=";
  130.     strcat(connString, xyz);
  131.     strcpy(fileName, argv[1]);
  132.     strncpy(tableName, fileName, strcspn(fileName, "."));
  133.     connection = PQconnectdb(connString);
  134.  
  135.     if(PQstatus(connection) == CONNECTION_OK) {
  136.         printf("connection made\n");
  137.     }
  138.     else
  139.     {
  140.         printf("connection failed: %s\n", PQerrorMessage(connection));
  141.         return 1;
  142.     }
  143.  
  144.     FILE* file = fopen(fileName, "r");
  145.     if (file == NULL){
  146.         printf("Nie mozna otworzyc pliku.");
  147.         return 1;
  148.     }
  149.     lineFirstChar = fgets(line, 150, file);
  150.     if (lineFirstChar != NULL) buildTable(tableName, lineFirstChar);
  151.  
  152.     do{
  153.         lineFirstChar = fgets(line, 150, file);
  154.         if (lineFirstChar != NULL) addEntry(tableName, lineFirstChar);
  155.     }while (lineFirstChar != NULL);
  156.  
  157.     fclose(file);
  158.  
  159.     printf("Test: %s", tableName);
  160.     return 0;
  161. }
Add Comment
Please, Sign In to add comment