Advertisement
Guest User

Untitled

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