Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 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. int main() {
  40. char ch;
  41. char nazwa[30];
  42. char uzytkownik[30];
  43. char haslo[30]={'\0'};
  44. char logData[150];
  45. login:
  46. strcpy(logData,"host=localhost port=5432 dbname=");
  47. printf("\npodaj nazwe bazy:\n");
  48. scanf("%s", nazwa);
  49. strcat(logData,nazwa);
  50. strcat(logData," user=");
  51. printf("\npodaj nazwe uzytkownika:\n");
  52. scanf("%s", uzytkownik);
  53. strcat(logData,uzytkownik);
  54. strcat(logData," password=");
  55. printf("\npodaj haslo:\n");
  56. system("stty -echo");
  57. scanf("%s", haslo);
  58. system("stty echo");
  59. system("clear");
  60. strcat(logData,haslo);
  61. PGconn *myconnection = PQconnectdb(logData);
  62. if(PQstatus(myconnection)==CONNECTION_OK){
  63. printf("polaczono!\n");
  64. printf("PGDBNAME =%s\n",PQdb(myconnection));
  65. printf("PGUSER =%s\n",PQuser(myconnection));
  66. printf("PGHOST =%s\n",PQhost(myconnection));
  67. printf("PGPORT =%s\n",PQport(myconnection));
  68. printf("OPTIONS =%s\n",PQoptions(myconnection));
  69. sleep(3);
  70. poczatek:
  71. system("clear");
  72. char naglowek[150];
  73. char liczba[5];
  74. int dlugoscvarchar=20;
  75. char tabela[30]={'\0'};
  76. char plik[34]={'\0'};
  77. int wybormenu=0;
  78. printf("\nWitaj! co chcesz zrobic?\n 1-dodaj tabele z pliku\n 2-dodaj rekord do bazy danych\n 3-koniec\n");
  79. scanf("%d", &wybormenu);
  80. switch (wybormenu) {
  81. case 1:
  82.  
  83. system("clear");
  84. printf("\nPodaj nazwe tabeli:\n");
  85. scanf("%s", tabela);
  86. strcpy(plik,tabela);
  87. strcat(plik,".csv");
  88.  
  89.  
  90.  
  91. FILE *plikcsv=fopen(plik,"r");
  92. if((plikcsv=fopen(plik, "r"))==NULL) {
  93. printf("Nie moge otworzyc pliku \"%s\", podaj poprawna nazwe!\n",plik);
  94. sleep(2);
  95. goto poczatek;
  96. }
  97. else {
  98. char drop[150]="DROP TABLE ";
  99. strcat(drop,tabela);
  100. doSQL(myconnection, drop);
  101.  
  102.  
  103. //Pobierz naglowek
  104. for (int i=0; i<1; i++) {
  105. fscanf(plikcsv, "%s", naglowek );
  106.  
  107. printf("\n%s\n", naglowek);
  108.  
  109. }
  110.  
  111. //masz jednego stringa, chcesz go podzielic i zapisac w tablice,
  112. //zeby w petli insertujacej dzialalo uniwersalnie
  113.  
  114.  
  115.  
  116.  
  117. //liczy przecinki w naglowku, wysteruj petle insert into values/create table
  118. int ileslow=1;
  119. for (int i=0; naglowek[i]; i++){
  120. ileslow += (naglowek[i] == ',');
  121. }
  122. printf("sa %d slowa\n",ileslow );
  123.  
  124.  
  125.  
  126.  
  127. //liczylinie z naglowkiem
  128. char linia[150];
  129. int ilelinii=0;
  130. while (fgets(linia, sizeof(linia), plikcsv)) {
  131. ilelinii=ilelinii+1;
  132. printf("jest %d lini", ilelinii);
  133. }
  134.  
  135.  
  136. //mały kuntakinte, ktory oddziela i liczy slowa :))
  137.  
  138. char createtable[150]="CREATE TABLE ";
  139. strcat(createtable,tabela);
  140. strcat(createtable,"(");
  141. char* token;
  142. char* string;
  143. char* tofree;
  144. int i=0;
  145. string = strdup(naglowek);
  146. if (string != NULL) {
  147. tofree = string;
  148. while ((token = strsep(&string, ",")) != NULL)
  149. {
  150. if (i==0){
  151. sprintf(liczba, "%d", dlugoscvarchar);
  152. strcat(createtable,token);
  153. strcat(createtable, " VARCHAR(");
  154. strcat(createtable, liczba);
  155. strcat(createtable, ") UNIQUE");
  156. }
  157. if (i!=0){
  158. sprintf(liczba, "%d", dlugoscvarchar);
  159. strcat(createtable,", ");
  160. strcat(createtable,token);
  161. strcat(createtable, " VARCHAR(");
  162. strcat(createtable, liczba);
  163. strcat(createtable, ")");
  164. }
  165. i=i+1;
  166. }
  167. free(tofree);
  168. }
  169. strcat(createtable,")");
  170. printf("%s\n", createtable);
  171. doSQL(myconnection, createtable);
  172. // inser tinto
  173.  
  174.  
  175.  
  176. rewind(plikcsv);
  177. for (int i=0; i<ilelinii; i++) {
  178. char insertintotable[150]="INSERT INTO ";
  179. char pom[150];
  180. strcat(insertintotable, tabela);
  181. strcat(insertintotable, " values(");
  182.  
  183.  
  184. if (i==0){//pierwsza linia
  185. fscanf(plikcsv,"%s", pom);
  186. printf("\nNaglowek %s\n", pom); //dziala
  187. }
  188.  
  189.  
  190.  
  191.  
  192. if (i!=0){//linie z zawartoscia
  193. fscanf(plikcsv,"%s", pom);//pobiera linie
  194. string = strdup(pom);
  195. if (string != NULL) {
  196. tofree = string;
  197. int j=0;
  198. while ((token = strsep(&string, ",")) != NULL)
  199. {
  200. if (j==0){
  201. strcat(insertintotable, "\'");
  202. strcat(insertintotable,token);
  203. strcat(insertintotable, "\'");
  204. //if (strlen(token)>=dlugoscvarchar){
  205. //dlugoscvarchar=strlen(token)+5;
  206. //char alter[150]="alter table "
  207. //strcat(alter, tabela);
  208.  
  209. }
  210.  
  211.  
  212. if (j!=0){
  213. strcat(insertintotable, ", \'");
  214. strcat(insertintotable,token);
  215. strcat(insertintotable, "\'");
  216. }
  217. j=j+1;
  218. }
  219. }
  220. free(tofree);
  221. strcat(insertintotable, ")");
  222. printf("%s\n", insertintotable );
  223. doSQL(myconnection, insertintotable);
  224. // doSQL(myconnection, "SELECT * FROM abc");
  225. }
  226. }
  227.  
  228.  
  229. /*
  230. while (fgets(linia, sizeof(linia), plikcsv)) {
  231. ilelinii=ilelinii+1;
  232. printf("jest %d lini", ilelinii);
  233. }
  234. */
  235.  
  236.  
  237.  
  238.  
  239.  
  240. }//koniec else petli dzialania z plikiem
  241. fclose(plikcsv);
  242.  
  243. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  244. scanf(" %c", &ch);
  245. if ((ch=='x')||(ch=='X')){ PQfinish(myconnection);
  246. return EXIT_SUCCESS;}
  247. goto poczatek;
  248. break;//koniec case 1:
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. case 2:
  259. {
  260. // char showcolumns[150]=" SELECT column_name FROM information_schema.columns WHERE table_name ='abc' ";
  261. system("clear");
  262. printf("\nPodaj nazwe tabeli:\n");
  263. scanf("%s", tabela);
  264.  
  265.  
  266. //naglowek=?
  267. //nazwa=?
  268.  
  269.  
  270. // printf(" numer = "); scanf("%d",&value);
  271. // printf(" nazwa = "); scanf("%s",name);
  272. // getchar();
  273. // sprintf(zapytanie, "INSERT INTO number VALUES(%d, \'%s\')",value,name);
  274. //printf("%s\n",zapytanie);
  275. // doSQL(conn, zapytanie);
  276. // strcat(showcolumns, tabela);
  277. // printf("%s\n",
  278. // doSQL(myconnection, showcolumns);
  279. // );
  280. doSQL(myconnection, "SELECT * FROM abc;");
  281.  
  282. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  283. scanf(" %c", &ch);
  284. if ((ch=='x')||(ch=='X')){ PQfinish(myconnection);
  285. return EXIT_SUCCESS;}
  286. goto poczatek;
  287. break;
  288. }
  289. case 3:
  290. break;
  291. }//koniec switch/case
  292. }
  293. else
  294. printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
  295. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  296. scanf(" %c", &ch);
  297. if ((ch=='x')||(ch=='X')){
  298. PQfinish(myconnection);
  299. return EXIT_SUCCESS;}
  300. goto login;
  301.  
  302. PQfinish(myconnection);
  303. return EXIT_SUCCESS;
  304. } //Bartosz Ratajczyk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement