Results 1 to 10 of 10

Thread: SQL command and QString?

  1. #1
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SQL command and QString?

    I would like to get the last ID of the table (plandfile)
    I got the SQL code for it which gives me a nice single string under:

    Qt Code:
    1. SELECT id FROM plandfile WHERE id=(SELECT MAX(id) FROM plandfile)
    To copy to clipboard, switch view to plain text mode 

    what would be the best way to transform this to a QString??
    Is there a method of QSqlquery which return a String??
    Please help
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQL command and QString?


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

    SunnySan (11th September 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    I have tried to test it

    Qt Code:
    1. QVariant variant;
    2. QSqlQuery query;
    3. query.exec("SELECT id FROM plandfile WHERE id=(SELECT MAX(id) FROM plandfile)");
    4. variant= query.value(0);// also try with 1
    5. QString plandfileID;
    6.  
    7. plandfileID=variant.toString();
    8. QMessageBox::about(this, tr("Value of ID"),plandfileID);
    To copy to clipboard, switch view to plain text mode 

    but I receive nothing in the QString.
    I tested the SQL alone, works fine.
    No Error but I m not sure why it's not working???

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQL command and QString?

    You have to call QSqlQuery::next() before you start reading values.

  6. #5
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    Something like this:

    Qt Code:
    1. QSqlQuery query;
    2. QString plandfileID;
    3.  
    4. query.exec("SELECT id FROM plandfile WHERE id=(SELECT MAX(id) FROM plandfile)");
    5.  
    6. while (query.next()) {
    7. plandfileID = query.value(0).toString();
    8. }
    9.  
    10. QMessageBox::about(this, tr("Value of ID"),plandfileID);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to sadjoker for this useful post:

    SunnySan (11th September 2008)

  8. #6
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    This is how I get the last entry.

    Qt Code:
    1. QSqlQuery query("SELECT uid from main");
    2. query.last();
    To copy to clipboard, switch view to plain text mode 
    Then I set that to be the max of my uidSpinbox
    Qt Code:
    1. fvui.uidSpinBox->setMaximum(query.value(0).toInt());
    To copy to clipboard, switch view to plain text mode 
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    Quote Originally Posted by fnmblot View Post
    This is how I get the last entry.

    Qt Code:
    1. QSqlQuery query("SELECT uid from main");
    2. query.last();
    To copy to clipboard, switch view to plain text mode 
    Then I set that to be the max of my uidSpinbox
    You select the whole table to get one specific record? What a waste of resources. Also the database returns records in a random order.

    To get the maximum uid use: SELECT MAX(uid) from main

  10. The following user says thank you to jacek for this useful post:

    fnmblot (11th September 2008)

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    Quote Originally Posted by SunnySan View Post
    I got the SQL code for it which gives me a nice single string under:

    Qt Code:
    1. SELECT id FROM plandfile WHERE id=(SELECT MAX(id) FROM plandfile)
    To copy to clipboard, switch view to plain text mode 
    You don't need a subquery here. Just use "SELECT MAX(id) FROM plandfile" and you'll get the max id.

  12. The following user says thank you to jacek for this useful post:

    SunnySan (11th September 2008)

  13. #9
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL command and QString?

    thanks all
    the final working code
    Qt Code:
    1. QSqlQuery query;
    2. QString plandfileID;
    3. query.exec("SELECT MAX(id) FROM plandfile");
    4. while (query.next()) {
    5. plandfileID = query.value(0).toString();
    6. }
    7. QMessageBox::about(this, tr("Value of ID"),plandfileID);
    To copy to clipboard, switch view to plain text mode 
    where id is the primary key of the table plandfile

    if you improve the code please let me know
    Last edited by SunnySan; 11th September 2008 at 22:46.

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQL command and QString?

    Sure. Replace "while" with "if".

  15. The following user says thank you to wysota for this useful post:

    SunnySan (16th September 2008)

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
  •  
Qt is a trademark of The Qt Company.