Results 1 to 4 of 4

Thread: Help with QT, SQLite, Update Statement

  1. #1
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Help with QT, SQLite, Update Statement

    Hi,

    I think this is almost a nobrainer , but i am going to ask it anyway as I dont't want to waste any more time on this,

    I am using SQlite database in my application and i want to update a value in a table where the value is calculated and stored in a variable . i am wondering how to pass the variables value to the column i want to update ..

    my code looks like this

    Qt Code:
    1.  
    2. q.exec("SELECT PRESSURE_HIGH, PRESSURE_LOW FROM PRESSURE_TBL");
    3.  
    4. q.next();
    5.  
    6. sensorHigh = q.value(0).toFloat();
    7.  
    8. sensorLow = q.value(1).toFloat();
    9.  
    10. sensorRange = qAbs(sensorHigh)+qAbs(sensorLow);
    11.  
    12. currentanalogIP = ((analogInput)/12);
    13.  
    14. currentDP = (sensorRange*currentanalogIP)+sensorLow;
    15.  
    16. q.exec("UPDATE PRESSURE_TBL SET CURRENT_DP = currentDP ");
    17.  
    18. // I am stuck here ....
    To copy to clipboard, switch view to plain text mode 


    I am wondering how to pass the currentDP variable to update the column CURRENT_DP in the table

    any help would be highly appreciated,

    Thanks.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with QT, SQLite, Update Statement

    you're close...
    instead of literally assigning the variable 'currentDP' to the field 'CURRENT_DP', you need to assign its *value*. Look at how to bind values:
    http://doc.qt.nokia.com/latest/qsqlquery.html#details

    You can also do something like this (not tested as I'm not near an actual database):
    Qt Code:
    1. q.exec(QString("UPDATE PRESSURE_TBL SET CURRENT_DP = %1").arg(currentDP));
    To copy to clipboard, switch view to plain text mode 

    make sure that CURRENT_DB has the proper field type that matches the type of currentDP.

    good luck
    Last edited by schnitzel; 17th March 2011 at 18:37. Reason: correct typo

  3. The following user says thank you to schnitzel for this useful post:

    chetu1984 (17th March 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with QT, SQLite, Update Statement

    Thank you very much that worked and was of great help...

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help with QT, SQLite, Update Statement

    I wouldn't get into the habit of constructing SQL as strings: it is open to abuse if the data is in any way untrusted and can fall foul of things like single quotes in parameters. These issues are avoided with bindable parameters:
    Qt Code:
    1. if (qry.prepare("UPDATE PRESSURE_TBL SET CURRENT_DP = :current_dp")) {
    2. qry.bindValue(":current_dp", currentDP);
    3. if (qry.exec())
    4. qDebug() << "Woohoo!";
    5. else
    6. qDebug() << "Update failed";
    7. }
    8. else
    9. qDebug() << "Your SQL is broken";
    To copy to clipboard, switch view to plain text mode 
    If you are going to do the update many times (e.g. in a loop) then you should prepare() the query once outside the loop, and just update the bind variables and exec() each time through the loop.

  6. The following user says thank you to ChrisW67 for this useful post:

    ajo (29th November 2012)

Similar Threads

  1. Replies: 2
    Last Post: 29th September 2010, 17:44
  2. SQLITE - UPDATE query problem
    By Tomasz in forum Newbie
    Replies: 12
    Last Post: 5th September 2010, 20:27
  3. Replies: 0
    Last Post: 21st April 2010, 16:25
  4. confusion with a STATEMENT used frequently
    By salmanmanekia in forum Newbie
    Replies: 3
    Last Post: 11th June 2008, 20:54
  5. SmartFOSS Mission Statement
    By travlr in forum Qt-based Software
    Replies: 0
    Last Post: 16th April 2007, 05:41

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.