Results 1 to 12 of 12

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

  1. #1
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Can we use the "like" word in a setFilter method to filter records in a DB table?

    Hello,
    just one thing, can we use the "like" word in a setFilter method? Like this:

    Qt Code:
    1. QString idtext = findSentenceLineEdit->text();
    2. sentenceModel->setFilter(QString("text like \"%1\"").arg(idtext));
    To copy to clipboard, switch view to plain text mode 

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

    Thanks,
    Palma

  2. #2
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Can we use the "like" word in a setFilter method to filter records in a DB table?

    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:

    Qt Code:
    1. QString idtext = findSentenceLineEdit->text();
    2. sentenceModel->setFilter(QString("text like \"%1%\"").arg(idtext));
    3. sentenceModel->select();
    To copy to clipboard, switch view to plain text mode 

    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

  3. #3
    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: Can we use the "like" word in a setFilter method to filter records in a DB table?

    Use quotes instead of double quotes and, please, don't post the same question more than onece.

  4. #4
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    ok, i will not put the same question twice.
    It still doesn't work. My code :

    Qt Code:
    1. sentenceModel->setFilter(QString("text like \'%1%\'").arg(idtext));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    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).

  6. #6
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    I'm using a SQLite database, the content of idtext is "primeira" for example and the text field is of varchar(100) type

  7. #7
    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: Can we use the "like" word in a setFilter method to filter records in a DB table?

    Can you filter that table using LIKE when you issue the query from the sqlite console?

  8. #8
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    Quote Originally Posted by nagpalma View Post
    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:
    Qt Code:
    1. sentenceModel->setFilter(QString("text like \'%1%\'").arg(idtext));
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. sentenceModel->setFilter(QString("text like \'%%1%\'").arg(idtext));
    To copy to clipboard, switch view to plain text mode 

    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.

  9. #9
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    Yes jacek, i can filter that table using LIKE when i issue the query from the sqlite console.

  10. #10
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    mm78, i tried with

    Qt Code:
    1. sentenceModel->setFilter(QString("text like \'%%1%\'").arg(idtext));
    To copy to clipboard, switch view to plain text mode 

    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.

  11. #11
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    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?

  12. The following user says thank you to mm78 for this useful post:

    nagpalma (10th July 2007)

  13. #12
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can we use the "like" word in a setFilter method to filter records in a DB table?

    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

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.