Problema con QSqlQuery e size
-
ciao!
ho la necessità di contare il numero di records di una query, in quanto devo eseguire una determinata operazione nel caso fosse zero.
ho provato così:QSqlQuery query; query.prepare(strQuery); query.bindValue(0, conto); query.exec(); qDebug() << query.size();
ma restituisce sempre -1, anche nel caso di record trovati.
come posso fare?? -
ciao!
è una semplice SELECT.ho fatto così:
if (!query.next()) { qDebug() << "KO"; } else { while (query.next()) { ui->baseTable->insertRow(ui->baseTable->rowCount()); for (int i = 0; i < header.size(); ++i) { ui->baseTable->setItem(rows, i, new QTableWidgetItem(query.value(header.at(i)).toString())); } rows++; } }
però succede che se non trova record esce il KO (quindi corretto).
se li trova, non li visualizza nel while.
nello specifico sembra ne visualizzi uno in meno. -
ciao!
è una semplice SELECT.ho fatto così:
if (!query.next()) { qDebug() << "KO"; } else { while (query.next()) { ui->baseTable->insertRow(ui->baseTable->rowCount()); for (int i = 0; i < header.size(); ++i) { ui->baseTable->setItem(rows, i, new QTableWidgetItem(query.value(header.at(i)).toString())); } rows++; } }
però succede che se non trova record esce il KO (quindi corretto).
se li trova, non li visualizza nel while.
nello specifico sembra ne visualizzi uno in meno.@fermatqt
If someone wishes to translate this into Italian... :)Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.
Even if your query is good, you may not get information about size/number of rows. Does whatever SQL back-end you are using support returning this?
if (!query.next()) { qDebug() << "KO"; } else { while (query.next()) {
Are you aware that this code throws away the first row in the
if (!query.next())
? Thewhile (query.next())
only sees & counts row #2 onward.Ciao!
-
bool haAlmenoUnaRiga = false; for(;query.next();haAlmenoUnaRiga=true) { ui->baseTable->insertRow(ui->baseTable->rowCount()); for (int i = 0; i < header.size(); ++i) { QTableWidgetItem* tempItem = new QTableWidgetItem; tempItem->setData(Qt::EditRole,query.value(header.at(i))); ui->baseTable->setItem(rows, i, tempItem); } rows++; } if(!haAlmenoUnaRiga) qDebug() << "KO";
-
bool haAlmenoUnaRiga = false; for(;query.next();haAlmenoUnaRiga=true) { ui->baseTable->insertRow(ui->baseTable->rowCount()); for (int i = 0; i < header.size(); ++i) { QTableWidgetItem* tempItem = new QTableWidgetItem; tempItem->setData(Qt::EditRole,query.value(header.at(i))); ui->baseTable->setItem(rows, i, tempItem); } rows++; } if(!haAlmenoUnaRiga) qDebug() << "KO";
@VRonin said in Problema con QSqlQuery e size:
bool haAlmenoUnaRiga = false; for(;query.next();haAlmenoUnaRiga=true) { ui->baseTable->insertRow(ui->baseTable->rowCount()); for (int i = 0; i < header.size(); ++i) { QTableWidgetItem* tempItem = new QTableWidgetItem; tempItem->setData(Qt::EditRole,query.value(header.at(i))); ui->baseTable->setItem(rows, i, tempItem); } rows++; } if(!haAlmenoUnaRiga) qDebug() << "KO";
sembra funzionare alla perfezione.
grazie mille!!