Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <libpq-fe.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7.  
  8. void printMainMenu();
  9.  
  10. int getSelection(int max, int min, void (*menu)(void));
  11.  
  12. void doSQL(PGconn *conn, char *command);
  13.  
  14. PGconn* connToDb();
  15.  
  16. void addSiedziba(PGconn* conn);
  17.  
  18. void addPojazd(PGconn* conn);
  19.  
  20.  
  21.  
  22.  
  23. int main()
  24. {
  25. PGconn* conn = connToDb();
  26. int wybor = 0;
  27. if(PQstatus(conn) == CONNECTION_OK)
  28. {
  29. while(wybor != -1)
  30. {
  31. wybor = getSelection(1,9,&printMainMenu);
  32. switch(wybor)
  33. {
  34. case 1:
  35. addSiedziba(conn);
  36. break;
  37. case 2:
  38. addPojazd(conn);
  39. break;
  40. }
  41. }
  42. }
  43. }
  44.  
  45.  
  46. void doSQL(PGconn *conn, char *command)
  47. {
  48. PGresult *result;
  49.  
  50. result = PQexec(conn, command);
  51.  
  52.  
  53. switch(PQresultStatus(result)) {
  54. case PGRES_TUPLES_OK:
  55. {
  56. int m, n;
  57. int nrows = PQntuples(result);
  58. int nfields = PQnfields(result);
  59. //printf("number of rows returned = %d\n", nrows);
  60. //printf("number of fields returned = %d\n", nfields);
  61. for(m = 0; m < nrows; m++) {
  62. for(n = 0; n < nfields; n++)
  63. printf(" %s = %s ",PQfname(result, n),PQgetvalue(result, m, n));
  64.  
  65. //PQgetlength(result, m, n)
  66.  
  67. printf("\n");
  68. }
  69. }
  70. }
  71. PQclear(result);
  72. }
  73.  
  74. void printMainMenu()
  75. {
  76. //system("clear");
  77. printf("------------MENU------------\n");
  78. printf("Wybierz jedną z poniższych czynności\n");
  79. printf("1. Dodaj siedzibę\n");
  80. printf("2. Dodaj pojazd\n");
  81. printf("3. Edytuj siedzibę\n");
  82. printf("4. Edytuj pojazd\n");
  83. printf("5. Usuń siedzibę\n");
  84. printf("6. Usuń pojazd\n");
  85. printf("7. Wyswietl wszystkie siedziby\n");
  86. printf("8. Wyświetl pojazdy\n");
  87. printf("9. Wyszukaj pojazd\n");
  88. printf("q - wyjście\n");
  89. }
  90.  
  91.  
  92. PGconn* connToDb()
  93. {
  94. int i=0;
  95. char temp[30];
  96.  
  97. PGconn* conn = NULL;
  98.  
  99. for(i=0; i<3; i++)
  100. {
  101. char connStr[100]="host=localhost port=5432 dbname=";
  102. printf("Podaj nazwę bazy\n");
  103. scanf("%s",temp);
  104. strcat(connStr,temp);
  105. strcat(connStr, " user=");
  106. printf("Podaj nazwę użytkownika\n");
  107. scanf("%s",temp);
  108. strcat(connStr,temp);
  109. strcat(connStr, " password=");
  110. printf("Podaj hasło\n");
  111. struct termios term, term_orig;
  112. tcgetattr(STDIN_FILENO, &term);
  113. term_orig = term;
  114. term.c_lflag &= ~ECHO;
  115. tcsetattr(STDIN_FILENO, TCSANOW, &term);
  116. scanf("%s",temp);
  117. tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
  118. strcat(connStr, temp);
  119. conn = PQconnectdb(connStr);
  120. if(PQstatus(conn) == CONNECTION_OK)
  121. return conn;
  122. else
  123. if(i != 2)
  124. printf("Nie udało się połączyć z bazą, wpisza dane jeszcze raz. Pozostało prób: %d\n", 2-i);
  125. }
  126. return NULL;
  127. }
  128.  
  129. int getSelection(int min, int max, void (*menu)(void))
  130. {
  131. int wybor;
  132. while(1)
  133. {
  134. menu();
  135. while ( getchar() != '\n' );
  136. wybor = (int) getchar() - 48;
  137. if(wybor == 65)
  138. {
  139. wybor = -1;
  140. break;
  141. }
  142. if(wybor >= min && wybor <= max)
  143. {
  144. break;
  145. }
  146. }
  147. while ( getchar() != '\n' );
  148. return wybor;
  149. }
  150.  
  151. void addPojazd(PGconn* conn)
  152. {
  153. system("clear");
  154. printf("Dodajesz pojazd. Podaj\n");
  155. char insert[100] = "INSERT INTO pojazd VALUES(default,1,'Audi','A4',default,'WB 34NS');";
  156. }
  157.  
  158. void addSiedziba(PGconn* conn)
  159. {
  160. char temp[30];
  161. char insert[100] = "INSERT INTO siedziba VALUES(default,'";//Wąska',13,'Warszawa','00-901');";
  162. system("clear");
  163. printf("Dodajesz siedzibę. Podaj ulicę:\n");
  164. scanf(" %[^\n]",temp);
  165. strcat(insert, temp);
  166. strcat(insert, "',");
  167. printf("Podaj numer: \n");
  168. scanf(" %[^\n]",temp);
  169. strcat(insert, temp);
  170. strcat(insert, ",'");
  171. printf("Podaj miasto: \n");
  172. scanf(" %[^\n]",temp);
  173. strcat(insert, temp);
  174. strcat(insert, "','");
  175. printf("Podaj kod: \n");
  176. scanf(" %[^\n]",temp);
  177. strcat(insert, temp);
  178. strcat(insert, "');");
  179. printf("%s\n", insert);
  180. doSQL(conn, insert);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement