PDA

View Full Version : Can we use the "like" word in a setFilter method to filter records in a DB table?



nagpalma
9th July 2007, 18:58
Hello,
just one thing, can we use the "like" word in a setFilter method? Like this:


QString idtext = findSentenceLineEdit->text();
sentenceModel->setFilter(QString("text like \"%1\"").arg(idtext));

I tried but it seems that it doesn't work.

Thanks,
Palma

nagpalma
9th July 2007, 19:30
Hello,

can we use the "like" word in a setFilter method to filter table records based on the first letters a user put in a QLineEdit? Like this:


QString idtext = findSentenceLineEdit->text();
sentenceModel->setFilter(QString("text like \"%1%\"").arg(idtext));
sentenceModel->select();


I tried but it seems that it doesn't work, neither putting "=" instead of "like". I can only filter by one column of integer type.

Any help would be appreciated.
Thanks,
Palma

jacek
9th July 2007, 21:10
Use quotes instead of double quotes and, please, don't post the same question more than onece.

nagpalma
10th July 2007, 09:49
ok, i will not put the same question twice.
It still doesn't work. My code :


sentenceModel->setFilter(QString("text like \'%1%\'").arg(idtext));

mm78
10th July 2007, 11:05
It's probably because your LIKE clause isn't working as you expect it to. What kind of database are you connecting to? What's the content of the idtext argument and the text field in the database? Read up on LIKE in the database documentation.

Qt doesn't care what you pass to setFilter(). It just uses it to generate a select statement. If you feed it garbage it'll just pass the garbage to the database which will complain about it (and lastError() will return an error).

nagpalma
10th July 2007, 11:52
I'm using a SQLite database, the content of idtext is "primeira" for example and the text field is of varchar(100) type

jacek
10th July 2007, 13:54
Can you filter that table using LIKE when you issue the query from the sqlite console?

mm78
10th July 2007, 14:00
I'm using a SQLite database, the content of idtext is "primeira" for example and the text field is of varchar(100) type

The following code:

sentenceModel->setFilter(QString("text like \'%1%\'").arg(idtext));
will cause a select looking like this to be generated:
SELECT [...] FROM [...] WHERE text like 'primeira%'

This will only select the rows where text is exactly 'primeira'. If you want to select all rows containing 'primeira' you can try:


sentenceModel->setFilter(QString("text like \'%%1%\'").arg(idtext));

Selecting only rows starting with 'primeira' by using text LIKE 'primeira%' doesn't seem to work. This might be a SQLite bug, or it may be how SQLite works. Check the doc.

nagpalma
10th July 2007, 14:43
Yes jacek, i can filter that table using LIKE when i issue the query from the sqlite console.

nagpalma
10th July 2007, 14:54
mm78, i tried with


sentenceModel->setFilter(QString("text like \'%%1%\'").arg(idtext));

but it's the same thing, no record appear in the tableview associated with the sentenceModel. In the sqlite console the filter using "like" works with

select * from sentence where text like '%primeira%';

or with

select * from sentence where text like 'primeira%';

displaying one record as i expected.

mm78
10th July 2007, 15:33
Ok, then set a breakpoint at the end of QSqlRelationalQuery::selectStatement() and see what the generated query looks like - it might be bad. Did you check if lastError() returns an error or not?

nagpalma
10th July 2007, 15:58
thanks a lot mm78 , i checked if lastError() returns an error and it returned that the text field was ambiguous, i change the name and it worked. I think it's because text is a reserved word ( a datatype in sqlite).

Palma