If you're building the SQL query as you go along, and you decide at the last minute that you want to negate/ignore the "WHERE" part of your query, you can append OR 1
to your where-clause. Remember that logically, X OR TRUE is true for all X.
sqlite> SELECT id FROM moz_downloads WHERE id < 405 LIMIT 10;
80
403
404
sqlite> SELECT id FROM moz_downloads WHERE id < 405 OR 1 LIMIT 10;
80
403
404
405
407
408
409
410
411
412
Note that I had to stick a LIMIT 10
in there to not get too many results for the demonstration, but the second statement's where-clause is id < 405 or 1
.
It depends on the application, but you may or may not generate your queries at runtime. Some queries will always be the same, like SELECT * FROM recent_files
, but some queries will be like generated on-the-fly. In the latter case, you might have something like
something = make_safe_for_sql(get_something_from_user())
query = "SELECT * FROM data WHERE something=" + something
if should_ignore_something:
query += " OR 1"
database.execute(query)
Note: Depending on your SQL engine, you might need to do OR 1=1
to evaluate to a boolean true.