Dla uproszczenia przedstawiam szkielet warunku i pytanie jest takie - która wersja warunku jest lepsza i poprawnie napisana ?
if(lastID.exec()){
if((lastID.last() && lastID.isValid()) || (!lastID.last() && !lastID.isValid())){
if(...){
}
else{
(...)
}
}
czy poniższy warunek ? Dodam tylko, że w tym poniższym dubluję praktycznie te same instrukcje które są w warunku wyżej
if(lastID.exec()){
if(lastID.last() && lastID.isValid()){
if(...){
}
else{
(...)
}
}
else if(!lastID.last() && !lastID.isValid()){
if(...){
}
else{
(...)
}
}
}
Do podglądu umieszczę pełną wersję, która wygląda jak niżej
void SqlQueryModelLoanStatus::myAddRow()
{
QSqlQuery lastID, addRecord;
QString strGetID = "SELECT pIDstatusWypoz FROM tStatusWypoz";
lastID.prepare(strGetID);
QString strAddRecord = "INSERT INTO tStatusWypoz (pStatusWypoz) VALUES (:StatusWypozyczenia)";
if(lastID.exec()){
if(lastID.last() && lastID.isValid()){
if(this->pLoanStatus.isEmpty()){
emit messageOfNumber(1);
}
else{
addRecord.prepare(strAddRecord);
addRecord.bindValue(":StatusWypozyczenia",this->pLoanStatus);
addRecord.exec();
addRecord.clear();
this->pLoanStatus.clear();
refresh();
emit loanStatusIsSet();
emit messageOfNumber(0);
}
}
else if(!lastID.last() && !lastID.isValid()){
if(this->pLoanStatus.isEmpty()){
emit messageOfNumber(1);
}
else{
addRecord.prepare(strAddRecord);
addRecord.bindValue(":StatusWypozyczenia",this->pLoanStatus);
addRecord.exec();
addRecord.clear();
this->pLoanStatus.clear();
refresh();
emit loanStatusIsSet();
emit messageOfNumber(0);
}
}
}
else if(!lastID.exec()){
emit messageOfNumber(-1);
}
}
Jeżeli ktoś by pytał co to robi, to, to wstawia wiersze z tym, że dodałem poprawkę, która zawiera warunek z negacją, bo gdy tabela była pusta, to nie mogłem wstawić żadnego wiersza.