125
 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
  • 58,983
  • 20
  • 134
  • 177
sammiwei
  • 3,080
  • 8
  • 39
  • 51

4 Answers4

212

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
  • 58,983
  • 20
  • 134
  • 177
  • 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
9

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

AskNilesh
  • 63,753
  • 16
  • 113
  • 150
Alpha75
  • 1,783
  • 1
  • 24
  • 41
4

You can use <> operator

You will find here all the basic sql statements

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

AskNilesh
  • 63,753
  • 16
  • 113
  • 150
Amr Angry
  • 3,512
  • 1
  • 43
  • 36
0

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 ; Exclude All row with ayat_id = 0 : 
          quranCursor = db.rawQuery("SELECT * from alquran WHERE sura_id = '" + sooraId + "' AND ayat_id !='" + "0" + "'", null);
  // or,  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,248
  • 1
  • 14
  • 21