Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program tworzy nowa tabele w bazie danych z nazwa pliku podanego jako argument.
- * Autor: Przemyslaw Foltynski
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <libpq-fe.h>
- PGconn *connection;
- void doSQL(PGconn *conn, char *command)
- {
- PGresult *result;
- printf("%s\n", command);
- result = PQexec(conn, command);
- printf("status is : %s\n", PQresStatus(PQresultStatus(result)));
- printf("#rows affected: %s\n", PQcmdTuples(result));
- printf("result message: %s\n", PQresultErrorMessage(result));
- switch(PQresultStatus(result)) {
- case PGRES_TUPLES_OK:
- {
- int n = 0, m = 0;
- int nrows = PQntuples(result);
- int nfields = PQnfields(result);
- printf("number of rows returned = %d\n", nrows);
- printf("number of fields returned = %d\n", nfields);
- for(m = 0; m < nrows; m++) {
- for(n = 0; n < nfields; n++)
- printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
- printf("\n");
- }
- }
- }
- PQclear(result);
- }
- /*
- * Funkcja sluzy do budowania polecen SQL skladajacych sie z wielu lancuchow znakow.
- * @param dest Tablica do ktorej zostanie wpisane wygenerowane polecenie.
- * @param num Liczba dodatkowych argumentow (nie liczac dest i num).
- */
- void cmdBuilder(char dest[], int num, ...)
- {
- int i;
- dest[0] = '\0';
- va_list args;
- va_start(args, num);
- for (i = 0; i < num; i++)
- {
- strcpy(dest + strlen(dest), va_arg(args, char*));
- }
- va_end(args);
- }
- /*
- * Funkcja dodaje nowy wpis do bazy danych na podstawie lini zczytanej z pliku.
- * @param name Nazwa tabeli do ktorej zostanie dodany wpis.
- * @param entry Wiersz z pliku zawierajacy dane do dodania.
- */
- void addEntry(char* name, char* entry)
- {
- int i = 0;
- char colName[20];
- char** colData = (char**) malloc(sizeof(char*)*10);
- for (i = 0; i < 10; i++)
- colData[i] = (char*) malloc(sizeof(char)*20);
- char cmd[400];
- extractDataFromLine(colData, entry);
- 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], "')");
- doSQL(connection, cmd);
- }
- /*
- * Na podstawie wiersza z pliku wypelnia podana tabele polami oddzielonymi znakiem ';'
- */
- void extractDataFromLine(char* dest[], char* source)
- {
- int i = 0;
- char* pch;
- pch = strtok(source, ";");
- while (pch != NULL)
- {
- strcpy(dest[i], pch);
- pch = strtok(NULL, ";");
- i++;
- }
- }
- /*
- * Tworzy nowa tabele.
- * @param name Nazwa tabeli (jesli tabela o podanej nazwie juz istnieje to najpierw zostanie ona usunieta).
- * @param entry Wiersz z pliku na podstawie ktorego zostanie stworzona tabela.
- */
- void buildTable(char* name, char* entry)
- {
- int i = 0;
- char colName[20];
- char** colData = (char**) malloc(sizeof(char*)*10);
- for (i = 0; i < 10; i++)
- colData[i] = (char*) malloc(sizeof(char)*20);
- char cmd[400];
- extractDataFromLine(colData, entry);
- cmdBuilder(cmd, 2, "DROP TABLE ", name);
- doSQL(connection, cmd);
- 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))");
- doSQL(connection, cmd);
- }
- int main(int argc, char* argv[])
- {
- char line[150];
- char *lineFirstChar;
- char tableName[20];
- char fileName[20];
- char xyz[14] = { 99, 104, 97, 115, 108, 111, 119, 121, 103, 97, 115, 108, 111, '\0'};
- char connString[100] = "host=localhost port=5432 dbname=pfoltynski user=pfoltynski password=";
- strcat(connString, xyz);
- strcpy(fileName, argv[1]);
- strncpy(tableName, fileName, strcspn(fileName, "."));
- connection = PQconnectdb(connString);
- if(PQstatus(connection) == CONNECTION_OK) {
- printf("connection made\n");
- }
- else
- {
- printf("connection failed: %s\n", PQerrorMessage(connection));
- return 1;
- }
- FILE* file = fopen(fileName, "r");
- if (file == NULL){
- printf("Nie mozna otworzyc pliku.");
- return 1;
- }
- lineFirstChar = fgets(line, 150, file);
- if (lineFirstChar != NULL) buildTable(tableName, lineFirstChar);
- do{
- lineFirstChar = fgets(line, 150, file);
- if (lineFirstChar != NULL) addEntry(tableName, lineFirstChar);
- }while (lineFirstChar != NULL);
- fclose(file);
- printf("Test: %s", tableName);
- return 0;
- }
Add Comment
Please, Sign In to add comment