PDA

View Full Version : SQL trouble



xmeister
25th March 2009, 10:58
Can't get the SQL statement in this function to work. I've gone over and over it and rewritten it multiple times but just can't see what's wrong.

The function sets up a number of QActions for a popup menu which all works fine.
The commented lines are the new code added to retrieve the saved preferences from an SQLite database (sets whether actions are checked or not on launch) which is what's not working.

Every iteration of the for loop which calls userVars.value() errors with -
QSqlQuery::value: not positioned on a valid record.

Also debug of userVars.value(i) returns QVariant(,)

Heres the functions :



QMenu* Cust::f_cust_colFilterPopUp()
{
QSqlQuery userVars;
QStringList temp_details;
QString temp_checked;
temp_details << "Customer Id"<< "Surname"<< "Name"<< "Address"<< "City"<< "State"<< "Postcode"
<< "Country"<< "Business ph.1"<< "Home ph.1"<< "Business ph.2"<< "Home ph.2"
<<"Mobile"<< "Fax"<< "Scheduled Jobs"<< "Non Scheduled Jobs"<< "Incomplete Jobs"
<<"Total Jobs"<< "Jobs to Invoice"<< "Total Invoiced"<< "Unpayed Invoices";


//QString to replace * in SQL statement, using * for debuging atm
/*temp_checked << "rowid" << "showSurname"<< "showName"<< "showAddress"<< "showCity"<<
"showState"<< "showPcode"<< "showCountry"<< "showBPhone1"<<
"showHPhone1"<< "showBPhone2"<< "showHPhone2"<< "showMobPhone"<<
"showFax"<< "showScheduledJobs"<< "showUnscheduledJobs"<<
"showIncompleteJobs"<< "showTotalJobs"<< "showToInvoice"<<
"showInvoiced"<< "showOutInvoice";*/


if (!userVars.exec("SELECT * FROM savedData WHERE rowid=1")) //exec SQL and error check
{
QString error = userVars.lastError().text();
QMessageBox::critical(0, qApp->tr("Query Failed"),
qApp->tr("Unable to execute sql query.\n"
+ userVars.lastError().text().toAscii() +
"\n Click cancel to exit."), QMessageBox::Cancel);
}


qmenu_cust_popUpMenu = new QMenu;
for(int i=0;i<20;i++)
{
qaction_cust_popUpAction[i] = new QAction(qApp->activeWindow());
qaction_cust_popUpAction[i]->setCheckable(true);
qaction_cust_popUpAction[i]->setText(temp_details.at(i));

bool checked = userVars.value(i).toBool(); //get field and convert to bool
qaction_cust_popUpAction[i]->setChecked(checked); //set checkbox value

qmenu_cust_popUpMenu->addAction(qaction_cust_popUpAction[i]);
connect(qaction_cust_popUpAction[i], SIGNAL(changed()), this, SLOT(sl_cust_filter()));

qDebug()<<(userVars.value(i)); //debug returns QVariant(,)
}
return qmenu_cust_popUpMenu;
}

spirit
25th March 2009, 11:08
after QSqlQuery::exec you must call QSqlQuery::next.
for Qt Assistant


bool QSqlQuery::next ()

Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the active state and isSelect() must return true before calling this function or it will do nothing and return false.

The following rules apply:

If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record.
If the result is currently located after the last record, there is no change and false is returned.
If the result is located somewhere in the middle, an attempt is made to retrieve the next record.
If the record could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned.

See also previous(), first(), last(), seek(), at(), isActive(), and isValid().

xmeister
25th March 2009, 11:53
Cheers,

I knew it'd be something simple, I actually tried that but must have had something else messed up at the time because it didn't help so I figured the query started on the first record.

All good now, thanks again.