0
I have the following problem, when executing the method insereOP()
the instances maq
and cli
lose their data.
So when I call the method atualizarOP
, client and machine information do not exist, generating error in SQL command.
ClienteDAO cliDAO(con);
Cliente * cli = cliDAO.getCliente(3);
MaquinaDAO maqDAO(con);
Maquina * maq = maqDAO.getMaquina(1);
OrdemDeProducao tmp("op001",cli,maq,1,10000,QDate(2014,9,1));
OrdemDeProducaoDAO dialogDAO(con);
if (dialogDAO.insereOP(tmp)){
std::cout << "Inserido" << std::endl;
tmp.setQuantidadeProgramada(50000);
if(dialogDAO.atualizaOP(tmp)) {
std::cout << "Atualizado" << std::endl;
}
}
Methods used:
Maquina *MaquinaDAO::getMaquina(int codigoMaquina) {
Maquina * retorno = NULL;
if(db.open()) {
query = QSqlQuery(db);
query.prepare("SELECT CodigoMaquina, NomeMaquina, StatusMaquina FROM Maquina WHERE CodigoMaquina = ?");
query.addBindValue(codigoMaquina);
if(!query.exec()){
std::cout << query.lastError().text().toStdString() << std::endl;
db.close();
return retorno;
} else {
if (query.first())
retorno = new Maquina(query.value(0).toInt(), query.value(1).toString(), query.value(2).toInt());
}
db.close();
} else {
std::cout << db.lastError().text().toStdString() << std::endl;
}
return retorno;
}
Cliente *ClienteDAO::getCliente(int codigoCliente) {
Cliente * retorno = NULL;
if(db.open()) {
query = QSqlQuery(db);
query.prepare("SELECT CodigoCliente, NomeCliente FROM Clientes WHERE CodigoCliente = ?");
query.addBindValue(codigoCliente);
if(!query.exec()){
std::cout << query.lastError().text().toStdString() << std::endl;
db.close();
return retorno;
} else {
if (query.first())
retorno = new Cliente(query.value(0).toInt(), query.value(1).toString());
}
db.close();
} else {
std::cout << db.lastError().text().toStdString() << std::endl;
}
return retorno;
}
bool OrdemDeProducaoDAO::insereOP(OrdemDeProducao op) {
if(db.open()) {
query = QSqlQuery(db);
query.prepare("INSERT INTO OrdemdeProducao (op, CodigoCliente, CodigoMaquina, ordem, QuantidadeProgramada, datadeentrega) VALUES (?,?,?,?,?,?)");
query.addBindValue(op.getOP());
query.addBindValue(op.getCliente()->getCodigoCliente());
query.addBindValue(op.getMaquina()->getCodigoMaquina());
query.addBindValue(op.getOrdem());
query.addBindValue(op.getQuantidadeProgramada());
query.addBindValue(op.getDataDeEntrega());
if(!query.exec()){
std::cout << query.lastError().text().toStdString() << std::endl;
db.close();
return false;
}
db.close();
return true;
} else {
std::cout << db.lastError().text().toStdString() << std::endl;
return false;
}
}
All project files can be found in the Github repository: https://github.com/LuanTavares/PointSW
What may be causing this loss of information ?
Evil pointers...
– karlphillip