Results 1 to 4 of 4

Thread: Multiple sql queries

  1. #1
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Multiple sql queries

    Hello all,

    I would like your help concerning multiple sql queries in qt.

    At the moment i am trying to insert in a table a value and the same time to check if this value is already exists in an other table. Could you please give me an example how this can be done?

    Please see below how i tried to do it. I always get "Invalid device ID".

    Qt Code:
    1. QSqlQuery query;
    2. QString strQuery;
    3.  
    4. strQuery="INSERT INTO User VALUES ( :name, :psw, :dID)";
    5.  
    6. query.bindValue( ":name", ui.RUserNameLineEdit->text() );
    7. query.bindValue( ":psw",ui.RPasswordLineEdit->text() );
    8.  
    9. if (ui.RUserNameLineEdit->text() == 0)
    10. {
    11. printf("Invalid UserName\n");
    12. return;
    13. }
    14. if (ui.RPasswordLineEdit->text() == 0)
    15. {
    16. printf("Invalid password\n");
    17. return;
    18. }
    19. if (ui.ConfirmPassword->text() == 0)
    20. {
    21. printf("Invalid password\n");
    22. return;
    23. }
    24.  
    25. if (ui.RClientLineEdit->text() == 0)
    26. {
    27. printf("Invalid password\n");
    28. return;
    29. }
    30.  
    31.  
    32. query.exec(strQuery);
    33.  
    34. strQuery="SELECT * from Clientwhere DeviceID='%1'";
    35.  
    36. if (ui.RClientLineEdit->text() != "DeviceID")
    37. {
    38. printf("Invalid DeviceID\n");
    39. return;
    40. }
    41.  
    42. query.exec(strQuery);
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple sql queries

    First query is incomplete. If You use placeholder :dID You must bind him value.
    Second query returns ALL records with DeviceID ending with digit 1.

  3. #3
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Multiple sql queries

    Thank you for your response! You are right i forgot to bind the value.

    Concerning the second query could you please tell me how i have to write it in order to take the first value of the Client table?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple sql queries

    Quote Originally Posted by fantom View Post
    Thank you for your response! You are right i forgot to bind the value.

    Concerning the second query could you please tell me how i have to write it in order to take the first value of the Client table?
    Try this SQL :
    Qt Code:
    1. SELECT * FROM Clientwhere ORDER BY here_def_of_sort LIMIT 1
    To copy to clipboard, switch view to plain text mode 
    but LIMIT is an extension of SQL language and in different databases it has different construction. This is correct for PostgreSQL and MySQL in example. For DB2 it is
    Qt Code:
    1. SELECT * FROM Clientwhere ORDER BY here_def_of_sort FETCH FIRST 1 ROWS ONLY
    To copy to clipboard, switch view to plain text mode 
    .

Similar Threads

  1. Qt Basic Queries
    By somnathbanik in forum Newbie
    Replies: 6
    Last Post: 2nd November 2010, 10:28
  2. Replies: 9
    Last Post: 19th November 2009, 09:18
  3. QTableView queries
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2009, 09:13
  4. Using LIKE in parameterized SQL queries
    By MikeG in forum Qt Programming
    Replies: 4
    Last Post: 2nd November 2009, 19:25
  5. SQL date queries
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 10: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.