Results 1 to 7 of 7

Thread: SQL Query Organization

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL Query Organization

    True, you are right! def much more readable. But i do have an other question.
    I can see the massive benefit of arranging the queries the way you explained. But how about inner joins? or a query from multible tables?

    For example i designed the database in the way that a user has a user_status. so i created a table called user_status_tbl. with an ID and a Description
    The user_tbl contains one column called user_status which referes to the user_status_tbl.

    How can i implement that without having to programm the SQLQuery in C++ and also take advantave of the simplicity and speed of an SQLQuery?

    Thanks again my friend, ou are so helpful!

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: SQL Query Organization

    Great, i'm glad I can help
    But how about inner joins? or a query from multible tables?

    For example i designed the database in the way that a user has a user_status. so i created a table called user_status_tbl. with an ID and a Description
    The user_tbl contains one column called user_status which referes to the user_status_tbl.

    How can i implement that without having to programm the SQLQuery in C++ and also take advantave of the simplicity and speed of an SQLQuery?
    You can still use SQL code to do cross-table selections and return this data in a data structure, for example:
    Qt Code:
    1. // extended version of User class
    2. class UserExt{
    3. public:
    4. UserExt(QString name, QString address, QString description...)
    5. :_name(name)
    6. ,_address(address)
    7. ,_description(description) ... {
    8. }
    9. QString name() const{ return _name; }
    10. QString address() const{ return _address; }
    11. QString description() const{ return _description; }
    12. // .. other access methods
    13. private:
    14. QString _name;
    15. QString _address;
    16. QString _description;
    17. ...
    18. };
    19.  
    20. // it is just a pseudocode but i think you get the idea
    21. User myClass::GetUser(int userId)
    22. {
    23. q.prepare("SELECT u.name,u.address,st.description FROM User as u, user_status_tbl as st, othertable as ot, ... WHERE u.ID = :userId AND u.id=st.id AND ot.id=st.id, AND ...;");
    24. q.bindValue(":id",userId);
    25. if (q.next()) {
    26. return UserExt(q.value("name").toString(), q.value("address").toString(), q.value("description").toString()...);
    27. }
    28. throw new NoSuchUserException; //.. or another way of reporting an error
    29. }
    To copy to clipboard, switch view to plain text mode 

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

    dk (14th January 2014)

  4. #3
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL Query Organization

    Hello stampede,

    very good idea with putting each query in an object. Much easier for further processing! Especialy the more complex queryies become more unified towards the actual application, fantastic!
    Thanks you very much for all your help and effort, i wish i could pay a beer for you my friend

Similar Threads

  1. Qt Creator Subfolder Organization
    By hiead in forum Newbie
    Replies: 3
    Last Post: 21st October 2013, 00:50
  2. Replies: 2
    Last Post: 13th January 2011, 18:36
  3. About tree organization...
    By Patrick Sorcery in forum Newbie
    Replies: 2
    Last Post: 3rd September 2010, 07:43
  4. SQL query
    By JD2000 in forum Newbie
    Replies: 4
    Last Post: 1st December 2009, 14:21

Tags for this Thread

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.