Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <libpq-fe.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6.  
  7. PGconn *myconnection;
  8. char logData[150];
  9. void logindane(){
  10.  
  11. char nazwa[30];
  12. char uzytkownik[30];
  13. char haslo[30]={'\0'};
  14.  
  15. strcpy(logData,"host=localhost port=5432 dbname=");
  16. printf("\npodaj nazwe bazy:\n");
  17. scanf("%s", nazwa);
  18. strcat(logData,nazwa);
  19. strcat(logData," user=");
  20. printf("\npodaj nazwe uzytkownika:\n");
  21. scanf("%s", uzytkownik);
  22. strcat(logData,uzytkownik);
  23. strcat(logData," password=");
  24. printf("\npodaj haslo:\n");
  25. system("stty -echo");
  26. scanf("%s", haslo);
  27. system("stty echo");
  28. system("clear");
  29. strcat(logData,haslo);
  30. myconnection = PQconnectdb(logData);
  31. }
  32.  
  33.  
  34.  
  35. void doSQL(PGconn *conn, char *command)
  36. {
  37. PGresult *result;
  38.  
  39. printf("%s\n", command);
  40.  
  41. result = PQexec(conn, command);
  42. printf("status is : %s\n", PQresStatus(PQresultStatus(result)));
  43. printf("#rows affected: %s\n", PQcmdTuples(result));
  44. printf("result message: %s\n", PQresultErrorMessage(result));
  45.  
  46. switch(PQresultStatus(result)) {
  47. case PGRES_TUPLES_OK:
  48. {
  49. int n = 0, m = 0;
  50. int nrows = PQntuples(result);
  51. int nfields = PQnfields(result);
  52. printf("number of rows returned = %d\n", nrows);
  53. printf("number of fields returned = %d\n", nfields);
  54. for(m = 0; m < nrows; m++) {
  55. for(n = 0; n < nfields; n++)
  56. printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
  57. printf("\n");
  58. }
  59. }
  60. }
  61. PQclear(result);
  62. }
  63.  
  64.  
  65.  
  66.  
  67. void htmlfunkcja(int argc,char *arg[]){
  68. PGresult *query;
  69. char *messg;
  70. char sql[200];
  71. printf("<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>Baza</title>\n</head>\n<body>\n");
  72.  
  73. for(int i=2;i<argc;i++){
  74. strcpy(sql,"SELECT * FROM ");
  75. strcat(sql,arg[i]);
  76. strcat(sql,";");
  77.  
  78. query=PQexec(myconnection,sql);
  79.  
  80. messg = PQresultErrorMessage(query);
  81.  
  82. if(strlen(messg)>0) printf("<div>wystapil blad %s</div>\n",messg);
  83.  
  84. if(PQresultStatus(query) == PGRES_TUPLES_OK){
  85. int NoRows = PQntuples(query);
  86. int NoFields = PQnfields(query);
  87. printf("<h2>%s</h2>\n",arg[i]);
  88. printf("<table>\n");
  89. printf("<tr>\n");
  90. for(int n = 0; n < NoFields; n++){
  91. printf("<th>%s</th>", PQfname(query,n));
  92. }
  93. printf("</tr>\n");
  94.  
  95. for(int m = 0; m < NoRows; m++){
  96. printf("<tr>\n");
  97. for(int n = 0; n < NoFields; n++){
  98. printf("<td>%s</td>\n", PQgetvalue(query,m,n));
  99. }
  100. printf("</tr>\n");
  101. }
  102. printf("</table>\n");
  103. }
  104. PQclear(query);
  105. }
  106. printf("</body>\n</html>\n");
  107. }
  108.  
  109.  
  110.  
  111. int main(int argc, char *argv[]) {
  112. char ch;
  113.  
  114. login:
  115. logindane();
  116. if (argc<3){
  117. if(PQstatus(myconnection)==CONNECTION_OK){
  118. printf("polaczono!\n");
  119. printf("PGDBNAME =%s\n",PQdb(myconnection));
  120. printf("PGUSER =%s\n",PQuser(myconnection));
  121. printf("PGHOST =%s\n",PQhost(myconnection));
  122. printf("PGPORT =%s\n",PQport(myconnection));
  123. printf("OPTIONS =%s\n",PQoptions(myconnection));
  124. sleep(3);
  125. poczatek:
  126. system("clear");
  127. char naglowek[150];
  128. char liczba[5];
  129. int dlugoscvarchar=20;
  130. char tabela[30]={'\0'};
  131. char plik[34]={'\0'};
  132. int wybormenu=0;
  133. printf("\nWitaj! co chcesz zrobic?\n 1-dodaj tabele z pliku\n 2-html\n 3-koniec\n");
  134. scanf("%d", &wybormenu);
  135. switch (wybormenu) {
  136. case 1:
  137.  
  138. system("clear");
  139. printf("\nPodaj nazwe tabeli:\n");
  140. scanf("%s", tabela);
  141. strcpy(plik,tabela);
  142. strcat(plik,".csv");
  143.  
  144.  
  145.  
  146. FILE *plikcsv=fopen(plik,"r");
  147. if((plikcsv=fopen(plik, "r"))==NULL) {
  148. printf("Nie moge otworzyc pliku \"%s\", podaj poprawna nazwe!\n",plik);
  149. sleep(2);
  150. goto poczatek;
  151. }
  152. else {
  153. char drop[150]="DROP TABLE ";
  154. strcat(drop,tabela);
  155. doSQL(myconnection, drop);
  156.  
  157.  
  158. //Pobierz naglowek
  159. for (int i=0; i<1; i++) {
  160. fscanf(plikcsv, "%s", naglowek );
  161.  
  162. printf("\n%s\n", naglowek);
  163.  
  164. }
  165.  
  166. //masz jednego stringa, chcesz go podzielic i zapisac w tablice,
  167. //zeby w petli insertujacej dzialalo uniwersalnie
  168.  
  169.  
  170.  
  171.  
  172. //liczy przecinki w naglowku, wysteruj petle insert into values/create table
  173. int ileslow=1;
  174. for (int i=0; naglowek[i]; i++){
  175. ileslow += (naglowek[i] == ',');
  176. }
  177. printf("sa %d slowa\n",ileslow );
  178.  
  179.  
  180.  
  181.  
  182. //liczylinie z naglowkiem
  183. char linia[150];
  184. int ilelinii=0;
  185. while (fgets(linia, sizeof(linia), plikcsv)) {
  186. ilelinii=ilelinii+1;
  187. printf("jest %d lini", ilelinii);
  188. }
  189.  
  190.  
  191. //mały kuntakinte, ktory oddziela i liczy slowa :))
  192. char createtable[150]="CREATE TABLE ";
  193. char chwilowe[50]={'\0'};
  194. strcat(createtable,tabela);
  195. strcat(createtable,"(");
  196. char tabnaglowkow[ileslow+1][50];
  197. char* token;
  198. char* string;
  199. char* tofree;
  200. int i=0;
  201. string = strdup(naglowek);
  202. if (string != NULL) {
  203. tofree = string;
  204. while ((token = strsep(&string, ",")) != NULL)
  205. {
  206. if (i==0){
  207. sprintf(liczba, "%d", dlugoscvarchar);
  208. printf("ocobiega%d\n",i );
  209. //&chwilowe[i]<=token;
  210. //strcat(&tabnaglowkow[i][49],chwilowe);
  211. strcat(createtable,token);
  212. strcat(createtable, " VARCHAR(");
  213. strcat(createtable, liczba);
  214. strcat(createtable, ") UNIQUE");
  215. }
  216. if (i!=0){
  217. sprintf(liczba, "%d", dlugoscvarchar);
  218. // strcat(&tabnaglowkow[i][50],token);
  219. strcat(createtable,", ");
  220. strcat(createtable,token);
  221.  
  222.  
  223. strcat(createtable, " VARCHAR(");
  224. strcat(createtable, liczba);
  225. strcat(createtable, ")");
  226. }
  227. i=i+1;
  228. }
  229. free(tofree);
  230. }
  231. strcat(createtable,")");
  232. printf("%s\n", createtable);
  233. doSQL(myconnection, createtable);
  234. // inser tinto
  235.  
  236. for (int i =0; i < ileslow+1; i=i+1)
  237. {
  238. strcpy(&tabnaglowkow[i][50],"");
  239. printf("%d\n",i );
  240.  
  241. printf("tupowinnybycwynikitabnaglowkow%s\n",tabnaglowkow[i] );
  242. }
  243. printf("tupowinnybycwynikitabnaglowkow%s\n",chwilowe );
  244.  
  245.  
  246.  
  247.  
  248.  
  249. rewind(plikcsv);
  250. for (int i=0; i<ilelinii; i++) {
  251. char insertintotable[150]="INSERT INTO ";
  252. char pom[150];
  253. strcat(insertintotable, tabela);
  254. strcat(insertintotable, " values(");
  255.  
  256.  
  257. if (i==0){//pierwsza linia
  258. fscanf(plikcsv,"%s", pom);
  259. printf("\nNaglowek %s\n", pom); //dziala poczatekkombinowania
  260.  
  261.  
  262.  
  263.  
  264. string = strdup(pom);
  265. if (string != NULL) {
  266. tofree = string;
  267. int licznik=0;
  268. while ((token = strsep(&string, ",")) != NULL)
  269. {
  270. strcat(&tabnaglowkow[licznik+1][50],token);
  271. licznik=licznik+1;
  272. }
  273. }
  274. free(tofree);
  275.  
  276. for (int i =0; i < ileslow; i=i+1)
  277. {
  278. puts(tabnaglowkow[i+1]);
  279. }
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. }//konieckombinowania
  312.  
  313.  
  314.  
  315.  
  316. if (i!=0){//linie z zawartoscia
  317. fscanf(plikcsv,"%s", pom);//pobiera linie
  318. string = strdup(pom);
  319. if (string != NULL) {
  320. tofree = string;
  321. int j=0;
  322. while ((token = strsep(&string, ",")) != NULL)
  323. {
  324. if (j==0){
  325. strcat(insertintotable, "\'");
  326. strcat(insertintotable,token);
  327. strcat(insertintotable, "\'");
  328. //if (strlen(token)>=dlugoscvarchar){
  329. //dlugoscvarchar=strlen(token)+5;
  330. //char alter[150]="alter table "
  331. //strcat(alter, tabela);
  332.  
  333. }
  334.  
  335.  
  336. if (j!=0){
  337. strcat(insertintotable, ", \'");
  338. strcat(insertintotable,token);
  339. strcat(insertintotable, "\'");
  340. }
  341. j=j+1;
  342. }
  343. }
  344. free(tofree);
  345. strcat(insertintotable, ")");
  346. printf("%s\n", insertintotable );
  347. doSQL(myconnection, insertintotable);
  348. // doSQL(myconnection, "SELECT * FROM abc");
  349. }
  350. }
  351. printf("\nczy dodac cos jeszcze? t/inny znak\n");
  352. scanf(" %c", &ch);
  353. if ((ch=='t')||(ch=='T')){
  354. char insertintotable[150]="INSERT INTO ";
  355. strcat(insertintotable, tabela);
  356. strcat(insertintotable, " values(");
  357. printf("podaj wartosci:\"%s\" oddzielone enterem",naglowek);
  358. for (int i=0;i<ileslow;i=i+1){
  359. char temp[150];
  360. if (i==0){
  361. scanf("%s\n",temp);
  362. strcat(insertintotable, "\'");
  363. strcat(insertintotable, temp);
  364. strcat(insertintotable, "\'");
  365. }
  366. if (i!=0){
  367. scanf("%s\n",temp);
  368. strcat(insertintotable, ", \'");
  369.  
  370. strcat(insertintotable, temp);
  371. strcat(insertintotable, "\'");
  372. }
  373. }
  374. strcat(insertintotable, ")");
  375. doSQL(myconnection, insertintotable);
  376. }
  377.  
  378. /*
  379. while (fgets(linia, sizeof(linia), plikcsv)) {
  380. ilelinii=ilelinii+1;
  381. printf("jest %d lini", ilelinii);
  382. }
  383. */
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398. //if (strlen(temp)>=dlugoscvarchar){
  399. //dlugoscvarchar=strlen(temp)+5;
  400.  
  401. //}
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. }//koniec else petli dzialania z plikiem
  433. fclose(plikcsv);
  434.  
  435. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  436. scanf(" %c", &ch);
  437. if ((ch=='x')||(ch=='X')){ PQfinish(myconnection);
  438. return EXIT_SUCCESS;}
  439. goto poczatek;
  440. break;//koniec case 1:
  441.  
  442. case 2:{
  443. /* system("clear");
  444. printf("\nPodaj nazwe tabeli:\n");
  445. scanf("%s", tabela);
  446. char nazwapliku[50];
  447. strcat( nazwapliku, tabela);
  448. strcat( nazwapliku, ".html");
  449. FILE *plikhtml = NULL;
  450.  
  451. fp = fopen(nazwapliku ,"a");
  452.  
  453. char choice;
  454.  
  455. if (fp != NULL) {
  456.  
  457. fprintf("<html><head><title>Last 5 Results</title></head><body><?php
  458. $connect = mysql_connect(\"localhost\",\"%s\",\"%s\");
  459. if (!$connect) {
  460. die(mysql_error());
  461. }
  462. mysql_select_db(\"%s\");
  463. $results = mysql_query(\"SELECT * FROM %s\");
  464. while($row = mysql_fetch_array($results)) { echo $row['Name'] . \"</br>\";
  465.  
  466.  
  467. ?>
  468. </body>
  469. </html>");
  470.  
  471.  
  472. */
  473. break;
  474. }
  475.  
  476.  
  477. case 3:
  478. break;
  479. }//koniec switch/case
  480. }
  481. else
  482. printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
  483. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  484. scanf(" %c", &ch);
  485. if ((ch=='x')||(ch=='X')){
  486. PQfinish(myconnection);
  487. return EXIT_SUCCESS;}
  488. goto login;
  489. }
  490.  
  491.  
  492. else {
  493.  
  494. int saved_stdout,temporary;
  495. saved_stdout=dup(1);
  496. dup2(temporary,1);
  497. //ConnHtml(strConnect,sizeof(strConnect),argv[1]);
  498. myconnection = PQconnectdb(logData);
  499.  
  500. dup2(saved_stdout,1);
  501. close(saved_stdout);
  502. htmlfunkcja(argc,argv);
  503. dup2(temporary,1);
  504. if(PQstatus(myconnection) == CONNECTION_OK){
  505. printf("\nUdalo sie utworzyc strone :)\n");
  506. } else printf("\nPolaczenie z baza danych nie zostalo nawiazane, strona jest pusta\n");
  507. }
  508.  
  509. PQfinish(myconnection);
  510. return EXIT_SUCCESS;
  511. } //Bartosz Ratajczyk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement