I'm trying to create a simple CRUD, but I'm having problems with the query method.
I wanted the method to display the following: Name, enrollment, course, phone and email in the first row and below the tuples information one by one in an organized way.
But it displays the information totally disorganized, a tuple is occupying more than one line, and the names of the columns in the table do not appear correctly: >IusingCodeBlocks17.12withtheGNUcompiler
Universitydatabasetables:
createtablealuno(nomevarchar(100)notnull,matriculaint,cursovarchar(50)notnull,telefonevarchar(20)notnull,emailvarchar(75),primarykey(matricula));createtableprofessor(nomevarchar(100)notnull,matriculaint,disciplinavarchar(50)notnull,telefonevarchar(20)notnull,emailvarchar(75),primarykey(matricula));
Globalvariables,includeanddefine:
#include<stdio.h>#include<windows.h>#include<winsock2.h>#include<mysql.h>#include<string.h>#defineNOME100#defineCURSO40#defineTEL20#defineEMAIL75MYSQLconexao;intstatusConexao;charquery[100];
Methods(Iputonlythosethatinvolvetheproblem):
voidcadastroAluno(void){mysql_init(&conexao);charnome[NOME];intmatricula;charcurso[CURSO];chartelefone[TEL];chareMail[EMAIL];system("cls");
printf("Cadastro de alunos\n\n");
setbuf(stdin,NULL);
printf("Digite o nome:");
fgets(nome,NOME,stdin);
nome[strcspn(nome,"\n")] = 0;
printf("Digite a matricula:");
scanf("%d",&matricula);
setbuf(stdin,NULL);
printf("Digite o curso:");
fgets(curso,CURSO,stdin);
curso[strcspn(curso,"\n")] = 0;
setbuf(stdin,NULL);
printf("Digite o telefone:");
fgets(telefone,TEL,stdin);
telefone[strcspn(curso,"\n")] = 0;
setbuf(stdin,NULL);
printf("Digite o e-mail:");
fgets(eMail,EMAIL,stdin);
eMail[strcspn(eMail,"\n")] = 0;
setbuf(stdin,NULL);
if(mysql_real_connect(&conexao,"localhost", "root", "", "universidade", 3306, NULL, 0)){
sprintf(query,"INSERT INTO aluno(nome,matricula,curso,telefone,email) values('%s','%d','%s','%s','%s');",nome,matricula,curso,telefone,eMail);
statusConexao = mysql_query(&conexao,query);
if(!statusConexao){
printf("Cadastro concluído com sucesso\nLinhas afetadas:%d\n",mysql_affected_rows(&conexao));
}else{
printf("Falha no cadastro.Tente novamente\n");
}
system("PAUSE");
mysql_close(&conexao);
}else{
printf("Falha na conexão ao banco de dados\n");
printf("Erro %d: %s\n", mysql_errno(&conexao), mysql_error(&conexao));
}
}
void consultaAlunoSim(void){
system("cls");
mysql_init(&conexao);
MYSQL_RES * resp;
MYSQL_ROW linhas;
MYSQL_FIELD * campos;
int contador;
char querySimples[] = "SELECT * FROM aluno;";
if(mysql_real_connect(&conexao, "localhost", "root", "", "universidade", 3306, NULL, 0)){
if(mysql_query(&conexao,querySimples)){
printf("Erro: %s\n:",mysql_error(&conexao));
}else{
resp = mysql_store_result(&conexao);
if(resp){
campos = mysql_fetch_field(resp);
for(contador = 0; contador < mysql_num_fields(resp); contador++){
printf("%s",(campos[contador].name));
if(mysql_num_fields(resp) > 1){
printf("\t");
}
}
printf("\n");
while((linhas = mysql_fetch_row(resp))!= NULL){
for(contador = 0; contador < mysql_num_fields(resp); contador++){
printf("%s\t",linhas[contador]);
}
printf("\n");
}
}
system("PAUSE");
mysql_free_result(resp);
mysql_close(&conexao);
}
}else{
printf("Falha na conexão ao banco de dados\n");
printf("Erro %d : %s\n",mysql_errno(&conexao),mysql_error(&conexao));
}
}