Advertisement
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 ch;
- char nazwa[30];
- char uzytkownik[30];
- char haslo[30]={'\0'};
- char logData[150];
- login:
- strcpy(logData,"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);
- poczatek:
- system("clear");
- char naglowek[150];
- char liczba[5];
- int dlugoscvarchar=20;
- char tabela[30]={'\0'};
- char plik[34]={'\0'};
- int wybormenu=0;
- printf("\nWitaj! co chcesz zrobic?\n 1-dodaj tabele z pliku\n 2-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);
- sleep(2);
- goto poczatek;
- }
- 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){
- sprintf(liczba, "%d", dlugoscvarchar);
- strcat(createtable,token);
- strcat(createtable, " VARCHAR(");
- strcat(createtable, liczba);
- strcat(createtable, ") UNIQUE");
- }
- if (i!=0){
- sprintf(liczba, "%d", dlugoscvarchar);
- strcat(createtable,", ");
- strcat(createtable,token);
- strcat(createtable, " VARCHAR(");
- strcat(createtable, liczba);
- strcat(createtable, ")");
- }
- i=i+1;
- }
- free(tofree);
- }
- strcat(createtable,")");
- printf("%s\n", createtable);
- doSQL(myconnection, createtable);
- // inser tinto
- rewind(plikcsv);
- for (int i=0; i<ilelinii; i++) {
- char insertintotable[150]="INSERT INTO ";
- char pom[150];
- strcat(insertintotable, tabela);
- strcat(insertintotable, " values(");
- if (i==0){//pierwsza linia
- fscanf(plikcsv,"%s", pom);
- printf("\nNaglowek %s\n", pom); //dziala
- }
- if (i!=0){//linie z zawartoscia
- fscanf(plikcsv,"%s", pom);//pobiera linie
- string = strdup(pom);
- if (string != NULL) {
- tofree = string;
- int j=0;
- while ((token = strsep(&string, ",")) != NULL)
- {
- if (j==0){
- strcat(insertintotable, "\'");
- strcat(insertintotable,token);
- strcat(insertintotable, "\'");
- //if (strlen(token)>=dlugoscvarchar){
- //dlugoscvarchar=strlen(token)+5;
- //char alter[150]="alter table "
- //strcat(alter, tabela);
- }
- if (j!=0){
- strcat(insertintotable, ", \'");
- strcat(insertintotable,token);
- strcat(insertintotable, "\'");
- }
- j=j+1;
- }
- }
- free(tofree);
- strcat(insertintotable, ")");
- printf("%s\n", insertintotable );
- doSQL(myconnection, insertintotable);
- // doSQL(myconnection, "SELECT * FROM abc");
- }
- }
- printf("\nczy dodac cos jeszcze? t/inny znak\n");
- scanf(" %c", &ch);
- if ((ch=='t')||(ch=='T')){
- char insertintotable[150]="INSERT INTO ";
- strcat(insertintotable, tabela);
- strcat(insertintotable, " values(");
- printf("podaj wartosci:\"%s\" oddzielone enterem",naglowek);
- for (int i=0;i<ileslow;i=i+1){
- char temp[150];
- if (i==0){
- scanf("%s\n",temp);
- strcat(insertintotable, "\'");
- strcat(insertintotable, temp);
- strcat(insertintotable, "\'");
- }
- if (i!=0){
- scanf("%s\n",temp);
- strcat(insertintotable, ", \'");
- strcat(insertintotable, temp);
- strcat(insertintotable, "\'");
- }
- }
- strcat(insertintotable, ")");
- doSQL(myconnection, insertintotable);
- }
- /*
- while (fgets(linia, sizeof(linia), plikcsv)) {
- ilelinii=ilelinii+1;
- printf("jest %d lini", ilelinii);
- }
- */
- if (strlen(temp)>=dlugoscvarchar){
- dlugoscvarchar=strlen(temp)+5;
- }
- }//koniec else petli dzialania z plikiem
- fclose(plikcsv);
- printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
- scanf(" %c", &ch);
- if ((ch=='x')||(ch=='X')){ PQfinish(myconnection);
- return EXIT_SUCCESS;}
- goto poczatek;
- break;//koniec case 1:
- case 2:
- break;
- }//koniec switch/case
- }
- else
- printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
- printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
- scanf(" %c", &ch);
- if ((ch=='x')||(ch=='X')){
- PQfinish(myconnection);
- return EXIT_SUCCESS;}
- goto login;
- PQfinish(myconnection);
- return EXIT_SUCCESS;
- } //Bartosz Ratajczyk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement