Elmer Elmer - 7 months ago 14
Python Question

How do I add multiple "NOT LIKE '%?%' in the WHERE clause of sqlite3 in python code?

I have a sqlite3 query that is executed inside python code. An example would be

SELECT word FROM table WHERE word NOT LIKE '%a%';


This would select all of the words where 'a' does not occur in the word. This I can get to work perfectly. The problem is if I want to further restrict the results to not include 'b' anywhere in the word. I am picturing something like this.

SELECT word FROM table WHERE word NOT IN ('%a%', '%b%', '%z%');


which this obviously does not work, but this is kinda the idea.
And just adding an AND clause is not really an option unless that is the only way to perform this. So this code is not an option.

SELECT word FROM table WHERE word NOT LIKE '%a%' AND NOT LIKE '%b%';


If this is the only option then I will have to work with that, but I was hoping for something else. Thanks in advance for the help!

Answer

If you use Sqlite's REGEXP support ( see the answer at Problem with regexp python and sqlite for how to do that ) , then you can do it easily in one clause:

SELECT word FROM table WHERE word NOT REGEXP '[abc]';