Guest User

Untitled

a guest
Mar 22nd, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 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 nazwa[30];
  41. char uzytkownik[30];
  42. char naglowek[150];
  43. char haslo[30]={'\0'};
  44. char logData[150]="host=localhost port=5432 dbname=";
  45. printf("\npodaj nazwe bazy:\n");
  46. scanf("%s", nazwa);
  47. strcat(logData,nazwa);
  48. strcat(logData," user=");
  49. printf("\npodaj nazwe uzytkownika:\n");
  50. scanf("%s", uzytkownik);
  51. strcat(logData,uzytkownik);
  52. strcat(logData," password=");
  53. printf("\npodaj haslo:\n");
  54. system("stty -echo");
  55. scanf("%s", haslo);
  56. system("stty echo");
  57. system("clear");
  58. strcat(logData,haslo);
  59. PGconn *myconnection = PQconnectdb(logData);
  60. if(PQstatus(myconnection)==CONNECTION_OK){
  61. printf("polaczono!\n");
  62. printf("PGDBNAME =%s\n",PQdb(myconnection));
  63. printf("PGUSER =%s\n",PQuser(myconnection));
  64. printf("PGHOST =%s\n",PQhost(myconnection));
  65. printf("PGPORT =%s\n",PQport(myconnection));
  66. printf("OPTIONS =%s\n",PQoptions(myconnection));
  67. sleep(3);
  68. system("clear");
  69. char tabela[30]={'\0'};
  70. char plik[34]={'\0'};
  71. int wybormenu=0;
  72. printf("\nWitaj! co chcesz zrobic?\n 1-dodaj tabele z pliku\n 2-dodaj rekord do bazy danych\n 3-koniec\n");
  73. scanf("%d", &wybormenu);
  74. switch (wybormenu) {
  75. case 1:
  76.  
  77. system("clear");
  78. printf("\nPodaj nazwe tabeli:\n");
  79. scanf("%s", tabela);
  80. strcpy(plik,tabela);
  81. strcat(plik,".csv");
  82.  
  83.  
  84.  
  85. FILE *plikcsv=fopen(plik,"r");
  86. if((plikcsv=fopen(plik, "r"))==NULL) {
  87. printf("Nie moge otworzyc pliku \"%s\", podaj poprawna nazwe!\n",plik);
  88. exit(1);
  89. }
  90. else {
  91. char drop[150]="DROP TABLE ";
  92. strcat(drop,tabela);
  93. doSQL(myconnection, drop);
  94.  
  95.  
  96. //Pobierz naglowek
  97. for (int i=0; i<1; i++) {
  98. fscanf(plikcsv, "%s", naglowek );
  99.  
  100. printf("\n%s\n", naglowek);
  101.  
  102. }
  103.  
  104. //masz jednego stringa, chcesz go podzielic i zapisac w tablice,
  105. //zeby w petli insertujacej dzialalo uniwersalnie
  106.  
  107.  
  108.  
  109.  
  110. //liczy przecinki w naglowku, wysteruj petle insert into values/create table
  111. int ileslow=1;
  112. for (int i=0; naglowek[i]; i++){
  113. ileslow += (naglowek[i] == ',');
  114. }
  115. printf("sa %d slowa\n",ileslow );
  116.  
  117.  
  118.  
  119.  
  120. //liczylinie z naglowkiem
  121. char linia[150];
  122. int ilelinii=0;
  123. while (fgets(linia, sizeof(linia), plikcsv)) {
  124. ilelinii=ilelinii+1;
  125. printf("jest %d lini", ilelinii);
  126. }
  127.  
  128.  
  129. //mały kuntakinte, ktory oddziela i liczy slowa :))
  130.  
  131. char createtable[150]="CREATE TABLE ";
  132. strcat(createtable,tabela);
  133. strcat(createtable,"(");
  134. char* token;
  135. char* string;
  136. char* tofree;
  137. int i=0;
  138. string = strdup(naglowek);
  139. if (string != NULL) {
  140. tofree = string;
  141. while ((token = strsep(&string, ",")) != NULL)
  142. {
  143. if (i==0){
  144. strcat(createtable,token);
  145. strcat(createtable," VARCHAR(20) UNIQUE");
  146. }
  147. if (i!=0){
  148. strcat(createtable,", ");
  149. strcat(createtable,token);
  150. strcat(createtable," VARCHAR(20)");
  151. }
  152. i=i+1;
  153. }
  154. free(tofree);
  155. }
  156. strcat(createtable,")");
  157. printf("%s\n", createtable);
  158. doSQL(myconnection, createtable);
  159. // inser tinto
  160.  
  161.  
  162. char insertintotable[150]="INSERT INTO ";
  163. char pom[150];
  164. strcat(insertintotable, tabela);
  165. strcat(insertintotable, " values(");
  166. for (int i=0; i<ilelinii; i++) {
  167.  
  168.  
  169.  
  170. fscanf(plikcsv,"%s", pom);
  171. printf("\nNaglowek %s\n", pom);
  172.  
  173.  
  174. /*
  175. while (fgets(linia, sizeof(linia), plikcsv)) {
  176. ilelinii=ilelinii+1;
  177. printf("jest %d lini", ilelinii);
  178. }
  179. */
  180.  
  181. }
  182.  
  183.  
  184.  
  185.  
  186. }//koniec else petli dzialania z plikiem
  187. fclose(plikcsv);
  188. break;//koniec case 1:
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. case 2:
  199. printf("\n Heheszki, nie umiem xD\n" );
  200.  
  201.  
  202. break;
  203. }//koniec switch/case
  204.  
  205. //create table tabela
  206.  
  207. //insert i jakies menu
  208.  
  209. //nie zawal pamieci niepotrzebnymi tablicami, odrazu wpisuj w sql i zeruj
  210.  
  211.  
  212. }
  213. else
  214. printf("wystapil blad %s\n",PQerrorMessage(myconnection) );
  215.  
  216. PQfinish(myconnection);
  217. return EXIT_SUCCESS;
  218. } //Bartosz Ratajczyk
Add Comment
Please, Sign In to add comment