Problema con QSqlQuery e size
-
wrote on 13 Jul 2018, 12:19 last edited by
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?? -
wrote on 13 Jul 2018, 14:42 last edited by VRonin
Che tipo di query e'? Se e' un
SELECT
to basta mettere unif(!query.next()) qDebug("0 risultati");
dopoquery.exec();
(che dovrebbe essere anch'esso in un if, comunque) -
wrote on 13 Jul 2018, 15:07 last edited by
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.wrote on 13 Jul 2018, 15:27 last edited by JonB@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!
-
wrote on 16 Jul 2018, 08:02 last edited by VRonin
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";
wrote on 17 Jul 2018, 09:47 last edited by@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!!
1/6