简体   繁体   中英

Query is returning only numbers or numbers+letters combination from table. How to modify it to show records with letters?

I want to receive records from column KEY_EXAMPLE only. When KEY_EXAMPLE column have records like "1234", or "1234a", code shows this kind of records, but when it have record like "abcd" it's not shown.

I tried to look why it don't want to show letters records, I think the problem is in "selection" but I don't know how to write it.

 public static final String _id = BaseColumns._ID;
 public static final String KEY_EXAMPLE = "Example";

 private static final String SCRIPT_CREATE_DATABASE =
        "create table " + MYDATABASE_TABLE + " ("
                + _id + " integer primary key autoincrement, "
                + KEY_X + " text, "
                + KEY_Y + " text,"
                + KEY_EXAMPLE + " text);";


 public Cursor showRecordsFromExample(){
    String[] selectedColumn = new String[]{_id, KEY_EXAMPLE};
    Cursor selectedCursor = sqLiteDatabase.query(MYDATABASE_TABLE, selectedColumn,
            "Example", null, null,
            null, null);
    return selectedCursor;
}

Why it's showing numbers or numbers+letters records only? How to make it showing letters records too?

Why it's showing numbers or numbers+letters records only?

The reason is that WHERE clause (3rd parameter of the query method) is very likely not the anticipated expression.

That is effectively you are using :-

SELECT _id,Example FROM mytable WHERE Example; 

So the Example column is considered to be the result of a comparison, it is either false or true (0 or 1).

As abcd in non-numeric it is 0 false.

When a number is encountered first eg 123 or 123ABC then SQlite considers the numeric value and if it is not 0 then it is true.

  • 0ABC would be false.
  • -9ABC wold be true.
  • ABC999 would be false (non-numerics first)

False rows (0) are ignored, true rows (non 0) are included and hence the result.

How to make it showing letters records too?

If you used :-

public Cursor showRecordsFromExample(){
    String[] selectedColumn = new String[]{_id, KEY_EXAMPLE};
    Cursor selectedCursor = sqLiteDatabase.query(MYDATABASE_TABLE, selectedColumn,
            null, null, null,
            null, null);
    return selectedCursor;
}

Then you would get values for all rows.

If you were to use :-

public Cursor showRecordsFromExample(){
    String[] selectedColumn = new String[]{_id, KEY_EXAMPLE};
    Cursor selectedCursor = sqLiteDatabase.query(MYDATABASE_TABLE, selectedColumn,
            KEY_EXAMPLE + " LIKE '%a%'", null, null,
            null, null);
}

Then this would return all rows that have an a or A anywhere in the Example column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM