127
 Cursor findNormalItems = db.query("items", columns, "type=?", 
                                   new String[] { "onSale" });

I want to return the cursor that points anything that are NOT onSale, what should I change? Thanks!

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
sammiwei
  • 3,140
  • 9
  • 41
  • 53

4 Answers4

216

From the official documentation:

The non-equals operator can be either != or <>

So your code becomes:

Cursor findNormalItems = db.query("items", columns, "type != ?", 
                                  new String[] { "onSale" });   
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • 9
    In my opinion, `!=` looks more professional - and is more consistent with the `=` and `==` operators. – ban-geoengineering Mar 23 '16 at 11:43
  • why I have to add "OR 'mycolumn' IS NOT NULL ? When I query with a where clause NOT EQUAL ? – ThierryC Jan 16 '18 at 11:09
  • 5
    @ban-geoengineering `<>` is SQL Ansi standard and `!=` is not. Of course `<>` is more professional – edc65 Jan 29 '18 at 21:49
  • @ban-geoengineering, here is [a bunch of comments](https://stackoverflow.com/a/723317) presenting arguments for usage of `!=` or `<>`. –  Jan 14 '19 at 11:39
10

You should use in the comparator the non-equal operator: "type!=?" or "type<>?".

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Alpha75
  • 2,140
  • 1
  • 26
  • 49
4

You can use <> operator

You will find here all the basic sql statements

http://www.firstsql.com/tutor2.htm

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
-1

With rawQuery :

quranCursor = db.rawQuery("SELECT * from alquran WHERE sura_id = '" + sooraId + "' AND ayat_id !='" + "0" + "'", null);

Usage:

     int sooraId = 3 ;
    
        try {
          // start from not 0, but 1 ; Include All row but ayat_id = 0 : 
          quranCursor = db.rawQuery("SELECT * from alquran WHERE sura_id = '" + sooraId + "' AND ayat_id !='" + "0" + "'", null);
  
        } catch (Exception e) {
          e.printStackTrace();
        }

This is tested one of my app, and works fine, Alhamdulillah.

Noor Hossain
  • 1,620
  • 1
  • 18
  • 25