SQLite3 Master/Detail | Can't get data in the Detail table
-
@JonB - I changed the first function to this:
void MainWindow::FillTable() { loading = true; QSqlQuery q(db); int r = 0, c = 0; qDebug()<< "Before SELECT"; if (!q.exec("SELECT recipe_id, name FROM recipes " "ORDER BY name")) db.lastError().text(); qDebug()<< "After SELECT"; qDebug() << r; ++r; qDebug() << r; qDebug() << q.isValid(); while (q.isValid()) // RETURNS FALSE EACH TIME NOW. THIS TABLE HAS 4 ENTRIES IN IT. { qDebug() << "Inside WHILE"; // ++r; ui->tw_recipes->insertRow(r); for (c = 0; c < 2; ++c) { qDebug() << c; qDebug()<< "Inside FOR"; ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString())); } } loading = false; }
My output is:
09:46:25: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs... Before SELECT After SELECT 0 1 false 09:46:34: /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs exited with code 0
Can anyone tell me why
while (q.isValid())
is failing? I see nothing wrong with my query. But obviously something's fouled up. Anyone?
Not sure how to post a pic here. So here's a link to show the table's valid entries: https://imgur.com/L29lGbb -
@landslyde
Hi
Why are you using q.isValid() and not
http://doc.qt.io/qt-5/qsqlquery.html#nextNormally to loop the result is like
QSqlQuery query("SELECT country FROM artist"); while (query.next()) { QString country = query.value(0).toString(); ... }
Did i miss something ?
-
@JonB - Your input was appreciated nonetheless. My initial code up there was a hack from a video I watched. While it worked, it wasn't even close to what I know Qt can offer. I hope you guys don't mind my questions. I'm full of them. Really enjoying Qt. Hands down, it's the best!
-
@landslyde
We basically hang around here for questions. ;)
So if you show code,
clearly state what you tried,
what you got and what you expected you will find us
very willing to try to help.Also the online Docs are excellent.
-
@mrjj - Thank you very much for the welcome. I do have one more question regarding all we've talked abt here. Although my initial post on this was sloppy code, it did fill the TableWidget properly. Now it won't load the four names I have in it.
void MainWindow::FillTable() { loading = true; QSqlQuery q(db); int r = 0, c = 0; if (!q.exec("SELECT recipe_id, name FROM recipes " "ORDER BY name")) db.lastError().text(); while (q.next()) { ++r; ui->tw_recipes->insertRow(r); for (c = 0; c < 2; ++c) { qDebug() << r; qDebug() << c; qDebug() << q.value(c); ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString())); } } loading = false; }
Output is:
10:55:47: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs... 1 0 QVariant(qlonglong, 3) 1 1 QVariant(QString, "Cat Finger Licks") 2 0 QVariant(qlonglong, 4) 2 1 QVariant(QString, "Chocolate Chip Cookies") 3 0 QVariant(qlonglong, 2) 3 1 QVariant(QString, "Lemon Cookies") 4 0 QVariant(qlonglong, 1) 4 1 QVariant(QString, "Oatmeal Pecan Chewy")
This line of code worked before, but now it doesn't:
ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
Why wld it not work? Is there something wrong with it? Please advise.
Pics of this: https://imgur.com/MsJ9xac and https://imgur.com/iQ4mGoI
UPDATE
I added a line to it and see that when I insert a row, nothing happens. Look:void MainWindow::FillTable() { loading = true; QSqlQuery q(db); int r = 0, c = 0; if (!q.exec("SELECT recipe_id, name FROM recipes " "ORDER BY name")) db.lastError().text(); while (q.next()) { ++r; ui->tw_recipes->insertRow(r); // NOTHING HAPPENS HERE for (c = 0; c < 2; ++c) { qDebug() << r; qDebug() << c; qDebug() << ui->tw_recipes->currentRow(); // I ADDED THIS LINE TO IT qDebug() << q.value(c); ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString())); } } loading = false; }
And the output:
11:18:16: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs... 1 0 -1 QVariant(qlonglong, 3) 1 1 -1 QVariant(QString, "Cat Finger Licks") 2 0 -1 QVariant(qlonglong, 4) 2 1 -1 QVariant(QString, "Chocolate Chip Cookies") 3 0 -1 QVariant(qlonglong, 2) 3 1 -1 QVariant(QString, "Lemon Cookies") 4 0 -1 QVariant(qlonglong, 1) 4 1 -1 QVariant(QString, "Oatmeal Pecan Chewy")
What's making it be
-1
? -
Hi
I assume TW is tableWidget ?
Please read this. it shows how its expected to be used.
https://wiki.qt.io/How_to_Use_QTableWidgetYou might need to tell it how many to expect/have
m_pTableWidget->setRowCount(10);
m_pTableWidget->setColumnCount(3);normally to add one new row, you can do
tableWidget->insertRow( tableWidget->rowCount() );The -1 is most likely
qDebug() << ui->tw_recipes->currentRow();
which means None selected. (since nothing been clicked on)also
++r; // is 1
ui->tw_recipes->insertRow(r); // ask it to add row at 1 but it dont have row zero so it cant. -
@mrjj - Finally got it! Thanks for all your help.
void MainWindow::FillTable() { loading = true; QSqlQuery q(db); int r = 0, c = 0; if (!q.exec("SELECT COUNT(*) FROM recipes")) db.lastError().text(); q.first(); int rCount = q.value(0).toInt(); if (!q.exec("SELECT recipe_id, name FROM recipes " "ORDER BY name")) db.lastError().text(); while (q.next()) { ui->tw_recipes->setRowCount(rCount); for (c = 0; c < 2; ++c) { ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString())); } ++r; } loading = false; }
-
Hi
super.
One note.
should it not be
int rCount = q.value(0).toInt();
ui->tw_recipes->setRowCount(rCount);instead of inside the while loop ?
-
@landslyde
Now be brave and remove the wholeSELECT COUNT(*)
and thesetRowCount()
, do it withinsertRow()
instead. Do it now! You know it makes sense ;-)Separately, your
db.lastError().text();
just returns the text, you won't see anything when yourexec()
s fail. You will want more like:qDebug << db.lastError().text(); return ...;
-
@JonB - I cldnt make
inserRow()
work. I'll mess around with it, but not just yet. Yes, it makes more sense doing it that way. I had the variabler
stuck in it:insertRow(r)
, which seems right to me. But it was DOA each time I ran it. I'll work with it though. And I appreciate all of your help, including the addition that came in while I was responding here, regarding using qDebug forlastError()
. Thanks.UPDATE
Works like a charm! =)void MainWindow::FillTable() { loading = true; QSqlQuery q(db); int r = 0, c = 0; if (!q.exec("SELECT recipe_id, name FROM recipes " "ORDER BY name")) qDebug() << db.lastError().text(); while (q.next()) { ui->tw_recipes->insertRow(r); for (c = 0; c < 2; ++c) { ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString())); } ++r; } loading = false; }