0
Good evening folks, I come to ask for help regarding the insertion and query of Mysql using C. I am trying to create a simple CRUD, however I am having problems with the query method.
I wanted the method to display the following: Name, enrollment, course, telephone and email in the first row and just below the tuple information, one by one in an organized way.
However it displays totally disorganized information, a tuple is occupying more than one row, and the names of the table columns do not appear correctly:
Me using Codeblocks 17.12 with the GNU compiler
University database tables:
create table aluno(
nome varchar(100) not null,
matricula int,
curso varchar(50) not null,
telefone varchar(20) not null,
email varchar(75),
primary key(matricula)
);
create table professor(
nome varchar(100) not null,
matricula int,
disciplina varchar(50) not null,
telefone varchar(20) not null,
email varchar(75),
primary key(matricula)
);
Global variables, include and define:
#include <stdio.h>
#include <windows.h>
#include <winsock2.h>
#include <mysql.h>
#include <string.h>
#define NOME 100
#define CURSO 40
#define TEL 20
#define EMAIL 75
MYSQL conexao;
int statusConexao;
char query[100];
Methods (I put only those that involve the problem):
void cadastroAluno(void){
mysql_init(&conexao);
char nome[NOME];
int matricula;
char curso[CURSO];
char telefone[TEL];
char eMail[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));
}
}
Look - I didn’t look at your code carefully - but why do you put a global variable "query" with only 100 bytes of buffer ?
char query[100];
- the first query you use, before replacing the parameters, already uses 88 bytes. - should go to close to 200 bytes made the substitutions. This may not even be responsible for the problems you are experiencing but it is a very serious problem that can make your program crash at any time. Put at least 4096 size for this query - on no PC will it make any difference– jsbueno
Thanks for the jsbueno remark. I should leave the global query variable like -> query[4096] ?
– Igor PTZ