Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <libpq-fe.h>
- #include <string.h>
- #include <stdio.h>
- #include <termios.h>
- #include <unistd.h>
- void printMainMenu();
- int getSelection(int max, int min, void (*menu)(void));
- void doSQL(PGconn *conn, char *command);
- PGconn* connToDb();
- void addSiedziba(PGconn* conn);
- void addPojazd(PGconn* conn);
- int main()
- {
- PGconn* conn = connToDb();
- int wybor = 0;
- if(PQstatus(conn) == CONNECTION_OK)
- {
- while(wybor != -1)
- {
- wybor = getSelection(1,9,&printMainMenu);
- switch(wybor)
- {
- case 1:
- addSiedziba(conn);
- break;
- case 2:
- addPojazd(conn);
- break;
- }
- }
- }
- }
- void doSQL(PGconn *conn, char *command)
- {
- PGresult *result;
- result = PQexec(conn, command);
- switch(PQresultStatus(result)) {
- case PGRES_TUPLES_OK:
- {
- int m, n;
- int nrows = PQntuples(result);
- int nfields = PQnfields(result);
- //printf("number of rows returned = %d\n", nrows);
- //printf("number of fields returned = %d\n", nfields);
- for(m = 0; m < nrows; m++) {
- for(n = 0; n < nfields; n++)
- printf(" %s = %s ",PQfname(result, n),PQgetvalue(result, m, n));
- //PQgetlength(result, m, n)
- printf("\n");
- }
- }
- }
- PQclear(result);
- }
- void printMainMenu()
- {
- //system("clear");
- printf("------------MENU------------\n");
- printf("Wybierz jedną z poniższych czynności\n");
- printf("1. Dodaj siedzibę\n");
- printf("2. Dodaj pojazd\n");
- printf("3. Edytuj siedzibę\n");
- printf("4. Edytuj pojazd\n");
- printf("5. Usuń siedzibę\n");
- printf("6. Usuń pojazd\n");
- printf("7. Wyswietl wszystkie siedziby\n");
- printf("8. Wyświetl pojazdy\n");
- printf("9. Wyszukaj pojazd\n");
- printf("q - wyjście\n");
- }
- PGconn* connToDb()
- {
- int i=0;
- char temp[30];
- PGconn* conn = NULL;
- for(i=0; i<3; i++)
- {
- char connStr[100]="host=localhost port=5432 dbname=";
- printf("Podaj nazwę bazy\n");
- scanf("%s",temp);
- strcat(connStr,temp);
- strcat(connStr, " user=");
- printf("Podaj nazwę użytkownika\n");
- scanf("%s",temp);
- strcat(connStr,temp);
- strcat(connStr, " password=");
- printf("Podaj hasło\n");
- struct termios term, term_orig;
- tcgetattr(STDIN_FILENO, &term);
- term_orig = term;
- term.c_lflag &= ~ECHO;
- tcsetattr(STDIN_FILENO, TCSANOW, &term);
- scanf("%s",temp);
- tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
- strcat(connStr, temp);
- conn = PQconnectdb(connStr);
- if(PQstatus(conn) == CONNECTION_OK)
- return conn;
- else
- if(i != 2)
- printf("Nie udało się połączyć z bazą, wpisza dane jeszcze raz. Pozostało prób: %d\n", 2-i);
- }
- return NULL;
- }
- int getSelection(int min, int max, void (*menu)(void))
- {
- int wybor;
- while(1)
- {
- menu();
- while ( getchar() != '\n' );
- wybor = (int) getchar() - 48;
- if(wybor == 65)
- {
- wybor = -1;
- break;
- }
- if(wybor >= min && wybor <= max)
- {
- break;
- }
- }
- while ( getchar() != '\n' );
- return wybor;
- }
- void addPojazd(PGconn* conn)
- {
- system("clear");
- printf("Dodajesz pojazd. Podaj\n");
- char insert[100] = "INSERT INTO pojazd VALUES(default,1,'Audi','A4',default,'WB 34NS');";
- }
- void addSiedziba(PGconn* conn)
- {
- char temp[30];
- char insert[100] = "INSERT INTO siedziba VALUES(default,'";//Wąska',13,'Warszawa','00-901');";
- system("clear");
- printf("Dodajesz siedzibę. Podaj ulicę:\n");
- scanf(" %[^\n]",temp);
- strcat(insert, temp);
- strcat(insert, "',");
- printf("Podaj numer: \n");
- scanf(" %[^\n]",temp);
- strcat(insert, temp);
- strcat(insert, ",'");
- printf("Podaj miasto: \n");
- scanf(" %[^\n]",temp);
- strcat(insert, temp);
- strcat(insert, "','");
- printf("Podaj kod: \n");
- scanf(" %[^\n]",temp);
- strcat(insert, temp);
- strcat(insert, "');");
- printf("%s\n", insert);
- doSQL(conn, insert);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement