Skip to content Skip to sidebar Skip to footer

Python: Sqlite OR Statement

Is there an OR sqlite statement? I've been trying to google it, but google seems to be filtering out the OR. For example if I wanted to find someone in this table (people) people:

Solution 1:

It would be one of those :

SELECT * FROM people WHERE fname IN ('Elvis', 'Richard');
SELECT * FROM people WHERE fname = 'Elvis' OR fname = 'Richard';

I'm surprised your first query even works, and in your second, or 'Richard' basically means or 1=1, which is always true, for every row in your table.

Your computer doesn't think like you do. Even if fname = 'Elvis' or 'Richard' seems obvious to you, the RDBMS doesn't know what you mean and reads "if fname is equal to 'Elvis' or... true". It evalutes the string to a boolean, and I assume since it's not empty, it's evaluated to true. And proceeds to select every row from your table, as I said earlier.


Solution 2:

"SQLite logical operators" was my query, and found you this: http://zetcode.com/databases/sqlitetutorial/expressions/

As to your actual results, you're comparing cows with donkeys :D. When you say:

select * from people where fname='Elvis' or 'Richard'

You're saying:

Give me all the rows from people that satisfy the following: it's true that fname equals Elvis, or it's true that Richard.

You're getting all the rows because Richard is considered true, being a non-empty string.

What you should do is:

select * from people where fname='Elvis' or fname='Richard'

Cheers!


Solution 3:

Try this:

select * from people where fname = 'Elvis' or  fname = 'Richard'

Solution 4:

OR is a logical statement that returns true or false.

So ("Elvis" or "Richard") returns probably false. And your actual SQL is really " fname = False ". most databases would throw an error but because sqlite internally follows TCL's "everything is a string" philosophy it is a valid statement.

What you need is simply:

select * from people where fname='Elvis' or fname =  'Richard'

Solution 5:

just a quick one, have you tried select * from people where fname='Elvis' or fname='Richard'


Post a Comment for "Python: Sqlite OR Statement"