PDA

View Full Version : LIMIT and OFFSET for QSqlRelationalTableModel



mkarakaplan
5th November 2007, 00:00
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?

wysota
5th November 2007, 00:20
setFilter should have worked. Did you remember to call select() after setting the filter?

mkarakaplan
5th November 2007, 00:30
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

musti
5th November 2007, 08:45
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?

wysota
5th November 2007, 09:28
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.

wysota
5th November 2007, 09:41
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().

musti
5th November 2007, 11:18
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.

wysota
5th November 2007, 12:56
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.

jacek
6th November 2007, 00:06
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}