Advertisement
Hayjerin

BD(corrigindo)

Jun 17th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.56 KB | None | 0 0
  1. /*
  2. Alterações feitas para funcionamento do programa cadastro de questões
  3.  
  4. alterar o encoding do banco de dados de UTF-8 para LATIN1:
  5.  
  6. update pg_database set encoding = pg_char_to_encoding('LATIN1')
  7. where datname = 'postgres';
  8.  
  9. trabalho com insert estabilizado:
  10.  
  11. https://pastebin.com/mDiuDcLE
  12. */
  13. /*
  14.     Command line to PostgreSQL database
  15.  */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <locale.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include "libpq-fe.h"
  22.  
  23.  
  24. /* IMPORTANT change this line to setup connection */
  25. #define DBDATA "host=localhost port=5432 dbname=postgres user=postgres password=postgres";
  26.     char query[500];
  27.     ////////////////////////
  28.     ////////VARIÁVEIS//////
  29.     //////////////////////
  30.     int dificuldade, temas[3], id_dominio, id_questao, id_tema, max=0;
  31.     char dominio[50], questao[255], resposta[255], tema[50];
  32.  
  33. const char *conninfo;
  34.     PGconn     *conn;
  35.     PGresult   *res;
  36.     int         nFields;
  37.     int         i, j;    
  38.    
  39. void print_tuplas(PGresult* res) {
  40.     int nFields = PQnfields(res);
  41.     int i, j;
  42.     for (i = 0; i < nFields; i++)
  43.         printf("%-15s", PQfname(res, i));
  44.     printf("\n");
  45.  
  46.     /* next, print out the rows */
  47.     for (i = 0; i < PQntuples(res); i++)
  48.     {
  49.         for (j = 0; j < nFields; j++)
  50.             printf("%-15s", PQgetvalue(res, i, j));
  51.         printf("\n");
  52.     }
  53. }  
  54.  
  55. void executarsql(){
  56.     res = PQexec(conn, query);
  57.  
  58.         /* handle the response */
  59.         switch (PQresultStatus(res)) {
  60.             case PGRES_EMPTY_QUERY:
  61.             case PGRES_COMMAND_OK: printf("Done.\n"); break;
  62.             case PGRES_TUPLES_OK: print_tuplas(res); break;
  63.             case PGRES_BAD_RESPONSE: printf("[ERROR] Bad Response"); break;
  64.             case PGRES_NONFATAL_ERROR:
  65.             case PGRES_FATAL_ERROR: printf(PQresultErrorMessage(res)); break;
  66.             default:
  67.                 printf("Unexpected Response");
  68.         }
  69.         /* clear the response pointer */
  70.         PQclear(res);
  71. }
  72. static void
  73. exit_nicely(PGconn *conn)
  74. {
  75.     PQfinish(conn);
  76.     exit(1);
  77. }
  78. void cadastrarquestao(){
  79.     do{
  80.         printf("\n\t\tCADASTRAR QUESTÃO\n");
  81.         printf("\n\tInforme qual o domínio da questão\n\tPara cadastrar um dominio digite 0\n\n");
  82.         sprintf(query,"SELECT * FROM tab_dominio;");
  83.         executarsql();
  84.         printf("\nDomínio da questão: ");
  85.         scanf("%d", &id_dominio);
  86.         setbuf(stdin, NULL);
  87.         if(id_dominio==0){
  88.             printf("\n\tCadastre um novo dominio\n");
  89.             printf("\tDigite o Dominio: ");
  90.             fgets(dominio, 50, stdin);
  91.             sprintf(query,"INSERT INTO tab_dominio (dominio) VALUES ('%s');", dominio);
  92.             executarsql();
  93.         }
  94.         setbuf(stdin, NULL);               
  95.     }while(id_dominio==0);
  96.     char loop='n';
  97.     do{
  98.         system("cls");
  99.         printf("\n\n\tCADASTRAR TEMAS\n");
  100.         sprintf(query,"SELECT id_tema, tema FROM tab_temas WHERE id_dominio = '%d';", id_dominio);
  101.         executarsql();
  102.         printf("\nDigite o numero do tema para a questão(MÁX de 4 temas).\nPara cadastrar um novo tema digite 0.\n\tOpção desejada: ");
  103.         scanf("%d", &id_tema);
  104.         setbuf(stdin, NULL);
  105.         if(id_tema == 0){
  106.             printf("\n\tDigite o Tema: ");
  107.             fgets(tema, 50, stdin);
  108.             sprintf(query,"INSERT INTO tab_temas (tema, id_dominio) VALUES ('%s','%d');", tema, id_dominio);
  109.             executarsql();
  110.         }else{
  111.         temas[max] = id_tema;
  112.         max++;
  113.         system("cls");
  114.         }
  115.         if(max==3){
  116.             printf("\n\tVocê já cadastrou o limite de temas para esta questão!");
  117.             system("pause");
  118.             break;
  119.         }
  120.         if(id_tema=='s'){
  121.             loop='n';
  122.         }
  123.     }while(loop=='s');
  124.     printf("Questão: ");
  125.     fgets(questao, 255, stdin);
  126.     setbuf(stdin, NULL);
  127.     printf("Nivel de dificuldade 1 a 5(1-Muito fácil, 5-Muito difícil): ");
  128.     scanf("%d", &dificuldade);
  129.     setbuf(stdin, NULL);
  130.     printf("Resposta da questão: ");
  131.     fgets(resposta, 255, stdin);
  132.     setbuf(stdin, NULL);  
  133.     sprintf(query, "INSERT INTO tab_questoes(pergunta, resposta, dificuldade, dominio) VALUES ('%s', '%s', %d, '%s');", questao, resposta, dificuldade, dominio);
  134.     executarsql();        
  135.     printf("\n\tQuestão cadastrada com sucesso!\n");
  136.     for(int a=0;a<max;a++){
  137.         sprintf(query,"INSERT INTO questaotema VALUES ('%s','%d');", questao, tema[a]);
  138.         executarsql();
  139.     }      
  140. }
  141. void buscar(){
  142.     int busca;
  143.     char procura[100], sair='n';
  144.     do{
  145.         system("cls");
  146.         printf("\n\t\tBUSCAR QUESTÃO\n");
  147.         printf("\n\t1-Texto da questao\n\t2-Dominio\n\t3-Tema\n\t4-Dificuldade ou 5-Sair");
  148.         scanf("%d", busca);
  149.         setbuf(stdin, NULL);
  150.         switch(busca){
  151.             case 1: printf("\nBuscar por texto na questão: \n");
  152.                     fgets(procura, 100, stdin);
  153.                     setbuf(stdin, NULL);
  154.                     //system("cls");
  155.                     sprintf(query,"SELECT pergunta, resposta FROM tab_questoes WHERE pergunta LIKE '%s';", procura);
  156.                     executarsql(); printf("\n\n"); system("pause"); break;
  157.             case 2: printf("\nBuscar por Dominio\n\n");
  158.                     sprintf(query,"SELECT * FROM dominio;");
  159.                     executarsql();
  160.                     printf("Digite o id do dominio: ");
  161.                     scanf("%d", &id_dominio);
  162.                     setbuf(stdin, NULL);
  163.                     sprintf(query, "SELECT pergunta, resposta FROM tab_questoes WHERE id_dominio = %d;", id_dominio);
  164.                     executarsql();
  165.                     printf("\n\n"); system("pause"); break;
  166.             case 3: sprintf(query,"SELECT * FROM tab_temas;");
  167.                     executarsql();
  168.                     printf("\nInforme o número do tema: ");
  169.                     scanf("%d", &id_tema);
  170.                     setbuf(stdin, NULL);
  171.                     //system("cls");
  172.                     sprintf(query,"SELECT tab_questoes.pergunta, tab_questoes.resposta FROM questaotema JOIN tab_questoes ON pergunta = pergunta JOIN tab_temas ON id_tema = id_tema WHERE id_tema = '%d';", id_tema);
  173.                     executarsql();
  174.                     printf("\n\n"); system("pause"); break;
  175.             case 4: printf("\nInforme a Dificuldade 1 à 5: ");
  176.                     scanf("%d", &dificuldade);
  177.                     //system("cls");
  178.                     sprintf(query,"SELECT * FROM tab_questoes WHERE dificuldade = %d;", dificuldade);
  179.                     executarsql();
  180.                     printf("\n\n"); system("pause"); break;
  181.             case 5: sair='s' ; break;
  182.         }
  183.     }while(sair=='n');
  184. }
  185.  
  186. int main(int argc, char **argv){    
  187.    
  188.     setlocale(LC_ALL, "portuguese");
  189.     int op;  
  190.     /*
  191.      * If the user supplies a parameter on the command line, use it as the
  192.      * conninfo string; otherwise default to setting dbname=postgres and using
  193.      * environment variables or defaults for all other connection parameters.
  194.      */
  195.      
  196.     if (argc > 1)
  197.         conninfo = argv[1];
  198.     else
  199.         conninfo = DBDATA;
  200.        
  201.     /* Make a connection to the database */
  202.     conn = PQconnectdb(conninfo);
  203.  
  204.     /* Check to see that the backend connection was successfully made */
  205.     if (PQstatus(conn) != CONNECTION_OK)
  206.     {
  207.         fprintf(stderr, "Connection to database failed: %s",
  208.                 PQerrorMessage(conn));
  209.         exit_nicely(conn);
  210.     }
  211.   //////////////////////////////////MEU CÓDIGO ABAIXO/////////////////////////////////////////////
  212.     int opcao;
  213.     do{
  214.         system("cls");
  215.         printf("\n\t\tBANCO DE QUESTÕES\n");
  216.         printf("\t1 - Cadastrar questão\n\t2 - Listar questões\n\t3 - Buscar questões\n\t4 - Sair");
  217.         printf("\n\tOpção: ");
  218.         scanf("%d", &opcao);
  219.         switch(opcao){
  220.             case 1: cadastrarquestao(); break;
  221.             case 2: sprintf(query,"SELECT tab_dominio.dominio, tab_questoes.pergunta, tab_questoes.resposta FROM tab_questoes JOIN tab_dominio ON tab_dominio.id_dominio = tab_questoes.id_dominio;");
  222.                     executarsql();
  223.                     printf("\n\n");
  224.                     break;
  225.             case 3: buscar(); break;
  226.             case 4: printf("saindo..."); exit_nicely(conn);
  227.             default: printf("\n\n\tOpção inválida\n\n\t");
  228.                      system("pause");
  229.                      system("cls");
  230.                      break;
  231.         }
  232.     }while(1);      
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement