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>
- PGconn *myconnection;
- char logData[150];
- void logindane(){
- char nazwa[30];
- char uzytkownik[30];
- char haslo[30]={'\0'};
- 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);
- myconnection = PQconnectdb(logData);
- }
- 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);
- }
- void htmlfunkcja(int argc,char *arg[]){
- PGresult *query;
- char *messg;
- char sql[200];
- printf("<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>Baza</title>\n</head>\n<body>\n");
- for(int i=2;i<argc;i++){
- strcpy(sql,"SELECT * FROM ");
- strcat(sql,arg[i]);
- strcat(sql,";");
- query=PQexec(myconnection,sql);
- messg = PQresultErrorMessage(query);
- if(strlen(messg)>0) printf("<div>wystapil blad %s</div>\n",messg);
- if(PQresultStatus(query) == PGRES_TUPLES_OK){
- int NoRows = PQntuples(query);
- int NoFields = PQnfields(query);
- printf("<h2>%s</h2>\n",arg[i]);
- printf("<table>\n");
- printf("<tr>\n");
- for(int n = 0; n < NoFields; n++){
- printf("<th>%s</th>", PQfname(query,n));
- }
- printf("</tr>\n");
- for(int m = 0; m < NoRows; m++){
- printf("<tr>\n");
- for(int n = 0; n < NoFields; n++){
- printf("<td>%s</td>\n", PQgetvalue(query,m,n));
- }
- printf("</tr>\n");
- }
- printf("</table>\n");
- }
- PQclear(query);
- }
- printf("</body>\n</html>\n");
- }
- int main(int argc, char *argv[]) {
- char ch;
- login:
- logindane();
- if (argc<3){
- 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-html\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);
- 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 ";
- char chwilowe[50]={'\0'};
- strcat(createtable,tabela);
- strcat(createtable,"(");
- char tabnaglowkow[ileslow+1][50];
- 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);
- printf("ocobiega%d\n",i );
- //&chwilowe[i]<=token;
- //strcat(&tabnaglowkow[i][49],chwilowe);
- strcat(createtable,token);
- strcat(createtable, " VARCHAR(");
- strcat(createtable, liczba);
- strcat(createtable, ") UNIQUE");
- }
- if (i!=0){
- sprintf(liczba, "%d", dlugoscvarchar);
- // strcat(&tabnaglowkow[i][50],token);
- 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
- for (int i =0; i < ileslow+1; i=i+1)
- {
- strcpy(&tabnaglowkow[i][50],"");
- printf("%d\n",i );
- printf("tupowinnybycwynikitabnaglowkow%s\n",tabnaglowkow[i] );
- }
- printf("tupowinnybycwynikitabnaglowkow%s\n",chwilowe );
- 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 poczatekkombinowania
- string = strdup(pom);
- if (string != NULL) {
- tofree = string;
- int licznik=0;
- while ((token = strsep(&string, ",")) != NULL)
- {
- strcat(&tabnaglowkow[licznik+1][50],token);
- licznik=licznik+1;
- }
- }
- free(tofree);
- for (int i =0; i < ileslow; i=i+1)
- {
- puts(tabnaglowkow[i+1]);
- }
- }//konieckombinowania
- 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:{
- /* system("clear");
- printf("\nPodaj nazwe tabeli:\n");
- scanf("%s", tabela);
- char nazwapliku[50];
- strcat( nazwapliku, tabela);
- strcat( nazwapliku, ".html");
- FILE *plikhtml = NULL;
- fp = fopen(nazwapliku ,"a");
- char choice;
- if (fp != NULL) {
- fprintf("<html><head><title>Last 5 Results</title></head><body><?php
- $connect = mysql_connect(\"localhost\",\"%s\",\"%s\");
- if (!$connect) {
- die(mysql_error());
- }
- mysql_select_db(\"%s\");
- $results = mysql_query(\"SELECT * FROM %s\");
- while($row = mysql_fetch_array($results)) { echo $row['Name'] . \"</br>\";
- ?>
- </body>
- </html>");
- */
- break;
- }
- case 3:
- 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;
- }
- else {
- int saved_stdout,temporary;
- saved_stdout=dup(1);
- dup2(temporary,1);
- //ConnHtml(strConnect,sizeof(strConnect),argv[1]);
- myconnection = PQconnectdb(logData);
- dup2(saved_stdout,1);
- close(saved_stdout);
- htmlfunkcja(argc,argv);
- dup2(temporary,1);
- if(PQstatus(myconnection) == CONNECTION_OK){
- printf("\nUdalo sie utworzyc strone :)\n");
- } else printf("\nPolaczenie z baza danych nie zostalo nawiazane, strona jest pusta\n");
- }
- PQfinish(myconnection);
- return EXIT_SUCCESS;
- } //Bartosz Ratajczyk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement