Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 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-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. printf("\nczy dodac cos jeszcze? t/inny znak\n");
  228. scanf(" %c", &ch);
  229. if ((ch=='t')||(ch=='T')){
  230. char insertintotable[150]="INSERT INTO ";
  231. strcat(insertintotable, tabela);
  232. strcat(insertintotable, " values(");
  233. printf("podaj wartosci:\"%s\" oddzielone enterem",naglowek);
  234. for (int i=0;i<ileslow;i=i+1){
  235. char temp[150];
  236. if (i==0){
  237. scanf("%s\n",temp);
  238. strcat(insertintotable, "\'");
  239. strcat(insertintotable, temp);
  240. strcat(insertintotable, "\'");
  241. }
  242. if (i!=0){
  243. scanf("%s\n",temp);
  244. strcat(insertintotable, ", \'");
  245.  
  246. strcat(insertintotable, temp);
  247. strcat(insertintotable, "\'");
  248. }
  249. }
  250. strcat(insertintotable, ")");
  251. doSQL(myconnection, insertintotable);
  252. }
  253.  
  254. /*
  255. while (fgets(linia, sizeof(linia), plikcsv)) {
  256. ilelinii=ilelinii+1;
  257. printf("jest %d lini", ilelinii);
  258. }
  259. */
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. if (strlen(temp)>=dlugoscvarchar){
  275. dlugoscvarchar=strlen(temp)+5;
  276.  
  277. }
  278.  
  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. }//koniec else petli dzialania z plikiem
  309. fclose(plikcsv);
  310.  
  311. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  312. scanf(" %c", &ch);
  313. if ((ch=='x')||(ch=='X')){ PQfinish(myconnection);
  314. return EXIT_SUCCESS;}
  315. goto poczatek;
  316. break;//koniec case 1:
  317.  
  318.  
  319. case 2:
  320. break;
  321. }//koniec switch/case
  322. }
  323. else
  324. printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
  325. printf("\nWprowadz dowolny znak, by kontynuowac lub x by wyjsc\n");
  326. scanf(" %c", &ch);
  327. if ((ch=='x')||(ch=='X')){
  328. PQfinish(myconnection);
  329. return EXIT_SUCCESS;}
  330. goto login;
  331.  
  332. PQfinish(myconnection);
  333. return EXIT_SUCCESS;
  334. } //Bartosz Ratajczyk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement