Results 1 to 3 of 3

Thread: SQL trouble

  1. #1

    Default SQL trouble

    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 :

    Qt Code:
    1. QMenu* Cust::f_cust_colFilterPopUp()
    2. {
    3. QSqlQuery userVars;
    4. QStringList temp_details;
    5. QString temp_checked;
    6. temp_details << "Customer Id"<< "Surname"<< "Name"<< "Address"<< "City"<< "State"<< "Postcode"
    7. << "Country"<< "Business ph.1"<< "Home ph.1"<< "Business ph.2"<< "Home ph.2"
    8. <<"Mobile"<< "Fax"<< "Scheduled Jobs"<< "Non Scheduled Jobs"<< "Incomplete Jobs"
    9. <<"Total Jobs"<< "Jobs to Invoice"<< "Total Invoiced"<< "Unpayed Invoices";
    10.  
    11.  
    12. //QString to replace * in SQL statement, using * for debuging atm
    13. /*temp_checked << "rowid" << "showSurname"<< "showName"<< "showAddress"<< "showCity"<<
    14.   "showState"<< "showPcode"<< "showCountry"<< "showBPhone1"<<
    15.   "showHPhone1"<< "showBPhone2"<< "showHPhone2"<< "showMobPhone"<<
    16.   "showFax"<< "showScheduledJobs"<< "showUnscheduledJobs"<<
    17.   "showIncompleteJobs"<< "showTotalJobs"<< "showToInvoice"<<
    18.   "showInvoiced"<< "showOutInvoice";*/
    19.  
    20.  
    21. if (!userVars.exec("SELECT * FROM savedData WHERE rowid=1")) //exec SQL and error check
    22. {
    23. QString error = userVars.lastError().text();
    24. QMessageBox::critical(0, qApp->tr("Query Failed"),
    25. qApp->tr("Unable to execute sql query.\n"
    26. + userVars.lastError().text().toAscii() +
    27. "\n Click cancel to exit."), QMessageBox::Cancel);
    28. }
    29.  
    30.  
    31. qmenu_cust_popUpMenu = new QMenu;
    32. for(int i=0;i<20;i++)
    33. {
    34. qaction_cust_popUpAction[i] = new QAction(qApp->activeWindow());
    35. qaction_cust_popUpAction[i]->setCheckable(true);
    36. qaction_cust_popUpAction[i]->setText(temp_details.at(i));
    37.  
    38. bool checked = userVars.value(i).toBool(); //get field and convert to bool
    39. qaction_cust_popUpAction[i]->setChecked(checked); //set checkbox value
    40.  
    41. qmenu_cust_popUpMenu->addAction(qaction_cust_popUpAction[i]);
    42. connect(qaction_cust_popUpAction[i], SIGNAL(changed()), this, SLOT(sl_cust_filter()));
    43.  
    44. qDebug()<<(userVars.value(i)); //debug returns QVariant(,)
    45. }
    46. return qmenu_cust_popUpMenu;
    47. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQL trouble

    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().
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3

    Default Re: SQL trouble

    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.

Similar Threads

  1. Trying to Connect To SQL Server 2005
    By rossd in forum Installation and Deployment
    Replies: 0
    Last Post: 6th March 2009, 19:56
  2. SQL Drivers Distribution
    By kandalf in forum Installation and Deployment
    Replies: 1
    Last Post: 12th January 2009, 09:17
  3. Can I use the Sql Module without chanage .pro file?
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 10:59
  4. Replies: 1
    Last Post: 4th October 2006, 16:05
  5. connect to sql server
    By raphaelf in forum Qt Programming
    Replies: 15
    Last Post: 27th February 2006, 18:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.