0
The data is loaded in the table, then I change the data in the table, when I click on the update button, I can take the data (even the changes) and save it in the variable, but it seems that when I enter the sql query nothing happens. I need to be able to update the table data as soon as I change something and click update button.
Connection to the bank(ok):
void ConectaBanco()
{
//Variavel do tipo DataBase
QSqlDatabase db;
//Conexao com o MYSQL
db = QSqlDatabase::addDatabase("QMYSQL");
//Servidor que vai receber o DB (padrão do server local)
db.setHostName("127.0.0.1");
//Porta que vai conectar com o DB (por padrão vai ser 3306)
db.setPort(3306);
//Seleciona o banco de dados
db.setDatabaseName("cursos");
//Usuario do BD
db.setUserName("root");
//Caso tenha senha no mysql
//db.setPassword();
//Teste de conexao
if(db.open())
{
qDebug()<<"Sucesso ao abrir a conexão";
}
else
{
qDebug()<<db.lastError().text();
}
}
Update function:
void MainWindow::on_pushButton_clicked()
{
int fila = 0;
ConectaBanco();
int CdAluno = ui->tabelaCarga->model()->data(ui->tabelaCarga->model()->index(fila,0)).toInt();
QString NmAluno = ui->tabelaCarga->model()->data(ui->tabelaCarga->model()->index(fila,1)).toString();
QString NmCurso = ui->tabelaCarga->model()->data(ui->tabelaCarga->model()->index(fila,2)).toString();
QString NmEscola = ui->tabelaCarga->model()->data(ui->tabelaCarga->model()->index(fila,3)).toString();
QSqlQuery query("UPDATE matricula set NmAluno= '"+NmAluno+"', "
"NmCurso= '"+NmCurso+"', "
"NmEscola='"+NmEscola+"' "
"WHERE CdMatricula='"+CdAluno+"'");
query.exec();
}
When I debug and place the mouse on top of the UPDATE string the following message appears: in Much value
I would like to know how to solve the problem and manage to change the data perfectly.