Results 1 to 5 of 5

Thread: Best practice to pass parameters to a QSqlQueryModel

  1. #1
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Best practice to pass parameters to a QSqlQueryModel

    Hello friends,
    can anyone state the best practices for passing one or more parameters to a QSqlQuery
    and specially to a QSqlQueryModel ?
    for example : you have a QString name , lastName
    query in english language : select Name , Lastname ,... from table people where Name = name and Lastname = lastName
    so that the query can take the name and last name from the users input and search of the record
    any help would be appreciated.
    Thanks in advance.

  2. #2
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best practice to pass parameters to a QSqlQueryModel

    I think there is only 2 ways to perform a QSqlQuery anyways, you either use either.

    Qt Code:
    1. QString name = "John";
    2. QString lastname = "Smith";
    3.  
    4. QString sql = "SELECT * FROM people WHERE Name = '" + name + "' AND LastName = '" + lastname + "'";
    5. QSqlQuery.exec(sql);
    To copy to clipboard, switch view to plain text mode 
    or
    you can prepare a statement then exec it..
    Qt Code:
    1. QSqlQuery.prepare("SELECT * FROM people WHERE Name = :name and LastName = :lastname);
    2. QSqlQuery.bindValue(":name", name);
    3. QSqlQuery.bindValue(":lastname", lastname);
    4. QSqlQuery.exec();
    To copy to clipboard, switch view to plain text mode 

    Note* Please correct me if there is another way, but this gets the job done for me.
    I only use Prepare statements when inserting data other than text, like QByteArray / Data.
    Last edited by zerokewl; 7th September 2013 at 12:16.

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

    toufic.dbouk (7th September 2013)

  4. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best practice to pass parameters to a QSqlQueryModel

    Hi there ,
    im not aware of any other methods to do that, basically i use the first suggestion but
    i was searching on other forums and i found out that that way doesn't take care of Sql Injections
    you can check that forum your self here http://forums.asp.net/t/1718846.aspx there is an argument about it
    so i started this thread to know what is the best way to do that in Qt using QSqlQueryModel and QSqlQuery
    i guess the 2nd suggestion takes care of the Sql Injections and some other bad habits
    thanks for your reply
    Last edited by toufic.dbouk; 7th September 2013 at 12:07.

  5. #4
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best practice to pass parameters to a QSqlQueryModel

    I normally use QSqlQueryModel this way,

    Qt Code:
    1. QString sql = "SELECT * From [myView] WHERE [NB]='144846'";
    2. model->setQuery(sql, databaseConnection);
    3. tableview->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    Last edited by zerokewl; 7th September 2013 at 12:15.

  6. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best practice to pass parameters to a QSqlQueryModel

    something like this might do the job:
    Qt Code:
    1. QSqlQuery Pqry;
    2. Pqry.prepare("SELECT * From [CarPlate09].[dbo].[myView] WHERE [ACTUALNB]=:number");
    3. Pqry.bindValue(":number",Number);
    4. Pqry.exec();
    5. model->setQuery(Pqry);
    6. QTableView *view = new QTableView;
    7. view->setModel(model);
    8. view->show();
    To copy to clipboard, switch view to plain text mode 

    yea i noticed that i didnt type the exec command but then i edited the post and fixed it
    thanks friend , keep in touch

    and friend use the code tags for better reading and understanding of code, it helps alot.
    best reagrds

Similar Threads

  1. How to pass parameters to a .ui file in Qt?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 7
    Last Post: 21st August 2012, 10:28
  2. Replies: 2
    Last Post: 12th June 2010, 02:21
  3. Replies: 4
    Last Post: 3rd May 2009, 18:32
  4. what's the better practice?
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 18:42
  5. Replies: 2
    Last Post: 16th August 2007, 00:20

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.