Results 1 to 9 of 9

Thread: LIMIT and OFFSET for QSqlRelationalTableModel

  1. #1
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default LIMIT and OFFSET for QSqlRelationalTableModel

    I want to add LIMIT and OFFSET SQL commands for QSqlRelationalTableModel.
    i tried to give with setFilter. but doesnt work.
    With setSort or setQuery i get following error.
    error: no matching function for call to ‘QSqlRelationalTableModel::setSort(QString)à ¢â‚¬â„¢

    What can i use for it?

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

    Default Re: LIMIT and OFFSET for QSqlRelationalTableModel

    setFilter should have worked. Did you remember to call select() after setting the filter?

  3. #3
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: LIMIT and OFFSET for QSqlRelationalTableModel

    I set filter like this
    fatModel->setFilter( tr("fat_id='%1' LIMIT 1000 OFFSET %2") .arg(lineEdit_fat_id->text()) . arg(lineEdit_offset->text()));

    but application sends postgresql server like this

    AND (fat_id='6' LIMIT 100 OFFSET 200)
    ^ Error is here

  4. #4
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Adding extra filters to QSqlRelationalTableModel

    I want to add to QSqlRelationalTableModel extra filters such as "LIMIT 100 OFFSET 200"
    I think it is possible only with setFilter I use
    myModel->setFilter("bills.firm_id=6 LIMIT 100 OFFSET 200")
    but application sends the query as follows.
    WHERE (firms.firm_id=bills.firm_id) AND (bills.firm_id=6 LIMIT 100 OFFSET 200)
    ofcourse this query doesnt work.
    How it works? Are there any other possibilities?

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

    Default Re: LIMIT and OFFSET for QSqlRelationalTableModel

    What is the exact code you use to create the model? And what is the point of using tr() in the filter argument? BTW. Your query is very insecure - it's vulnerable to SQL injection.

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

    Default Re: Adding extra filters to QSqlRelationalTableModel

    You can access the query object of the model (QSqlQueryModel::query()) and modify it in a way you want and then set it back using QSqlQueryModel::setQuery().

  7. #7
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: LIMIT and OFFSET for QSqlRelationalTableModel

    faturaModel= new QSqlRelationalTableModel(tableView);
    faturaModel->setTable("faturaayrinti");
    faturaModel->setFilter( QString(tr("fatura_id=%1 LIMIT %2")) .arg(lineEdit_fatura_id->text()) .arg(spinBox->text()));

    Here spinBox restricted as integer beetween 10-100. I think it is not vulnarable to sql injections.

    You has wrote
    "You can access the query object of the model (QSqlQueryModel::query()) and modify it in a way you want and then set it back using QSqlQueryModel::setQuery()."

    My model is QSqlRelationalTableModel setQuery doesnt work.

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

    Default Re: LIMIT and OFFSET for QSqlRelationalTableModel

    Quote Originally Posted by musti View Post
    My model is QSqlRelationalTableModel setQuery doesnt work.
    QSqlRelationalTableModel is derived from QSqlTableModel which is derived from QSqlQueryModel which has a QSqlQueryModel::setQuery member which you can access.

    And of course applying a proxy model to do the filtering and sorting is always an option too (you just waste some resources fetching rows you don't need from the database), although in your situation I'd simply use QSqlQueryModel instead of the relational model.

  9. #9
    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: Adding extra filters to QSqlRelationalTableModel

    Quote Originally Posted by musti View Post
    Are there any other possibilities?
    Either use QSqlQueryModel instead or try to exploit orderByClause().

    P.S. Please, don't start more than one thread on the same topic.

    {threads merged}

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.