Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <libpq-fe.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- 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);
- }
- int main() {
- char nazwa[30];
- char uzytkownik[30];
- char naglowek[150];
- char haslo[30]={'\0'};
- char logData[150]="host=localhost port=5432 dbname=";
- printf("\npodaj nazwe bazy:\n");
- scanf("%s", nazwa);
- strcat(logData,nazwa);
- strcat(logData," user=");
- printf("\npodaj nazwe uzytkownika:\n");
- scanf("%s", uzytkownik);
- strcat(logData,uzytkownik);
- strcat(logData," password=");
- printf("\npodaj haslo:\n");
- system("stty -echo");
- scanf("%s", haslo);
- system("stty echo");
- system("clear");
- strcat(logData,haslo);
- PGconn *myconnection = PQconnectdb(logData);
- if(PQstatus(myconnection)==CONNECTION_OK){
- printf("polaczono!\n");
- printf("PGDBNAME =%s\n",PQdb(myconnection));
- printf("PGUSER =%s\n",PQuser(myconnection));
- printf("PGHOST =%s\n",PQhost(myconnection));
- printf("PGPORT =%s\n",PQport(myconnection));
- printf("OPTIONS =%s\n",PQoptions(myconnection));
- sleep(3);
- system("clear");
- char tabela[30]={'\0'};
- char plik[34]={'\0'};
- int wybormenu=0;
- printf("\nWitaj! co chcesz zrobic?\n 1-dodaj tabele z pliku\n 2-dodaj rekord do bazy danych\n 3-koniec\n");
- scanf("%d", &wybormenu);
- switch (wybormenu) {
- case 1:
- system("clear");
- printf("\nPodaj nazwe tabeli:\n");
- scanf("%s", tabela);
- strcpy(plik,tabela);
- strcat(plik,".csv");
- FILE *plikcsv=fopen(plik,"r");
- if((plikcsv=fopen(plik, "r"))==NULL) {
- printf("Nie moge otworzyc pliku \"%s\", podaj poprawna nazwe!\n",plik);
- exit(1);
- }
- else {
- char drop[150]="DROP TABLE ";
- strcat(drop,tabela);
- doSQL(myconnection, drop);
- //Pobierz naglowek
- for (int i=0; i<1; i++) {
- fscanf(plikcsv, "%s", naglowek );
- printf("\n%s\n", naglowek);
- }
- //masz jednego stringa, chcesz go podzielic i zapisac w tablice,
- //zeby w petli insertujacej dzialalo uniwersalnie
- //liczy przecinki w naglowku, wysteruj petle insert into values/create table
- int ileslow=1;
- for (int i=0; naglowek[i]; i++){
- ileslow += (naglowek[i] == ',');
- }
- printf("sa %d slowa\n",ileslow );
- //liczylinie z naglowkiem
- char linia[150];
- int ilelinii=0;
- while (fgets(linia, sizeof(linia), plikcsv)) {
- ilelinii=ilelinii+1;
- printf("jest %d lini", ilelinii);
- }
- //mały kuntakinte, ktory oddziela i liczy slowa :))
- char createtable[150]="CREATE TABLE ";
- strcat(createtable,tabela);
- strcat(createtable,"(");
- char* token;
- char* string;
- char* tofree;
- int i=0;
- string = strdup(naglowek);
- if (string != NULL) {
- tofree = string;
- while ((token = strsep(&string, ",")) != NULL)
- {
- if (i==0){
- strcat(createtable,token);
- strcat(createtable," VARCHAR(20) UNIQUE");
- }
- if (i!=0){
- strcat(createtable,", ");
- strcat(createtable,token);
- strcat(createtable," VARCHAR(20)");
- }
- i=i+1;
- }
- free(tofree);
- }
- strcat(createtable,")");
- printf("%s\n", createtable);
- doSQL(myconnection, createtable);
- // inser tinto
- char insertintotable[150]="INSERT INTO ";
- char pom[150];
- strcat(insertintotable, tabela);
- strcat(insertintotable, " values(");
- for (int i=0; i<ilelinii; i++) {
- fscanf(plikcsv,"%s", pom);
- printf("\nNaglowek %s\n", pom);
- /*
- while (fgets(linia, sizeof(linia), plikcsv)) {
- ilelinii=ilelinii+1;
- printf("jest %d lini", ilelinii);
- }
- */
- }
- }//koniec else petli dzialania z plikiem
- fclose(plikcsv);
- break;//koniec case 1:
- case 2:
- printf("\n Heheszki, nie umiem xD\n" );
- break;
- }//koniec switch/case
- //create table tabela
- //insert i jakies menu
- //nie zawal pamieci niepotrzebnymi tablicami, odrazu wpisuj w sql i zeruj
- }
- else
- printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
- PQfinish(myconnection);
- return EXIT_SUCCESS;
- } //Bartosz Ratajczyk
Add Comment
Please, Sign In to add comment