[英]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.我只想从 KEY_EXAMPLE 列接收记录。 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.当 KEY_EXAMPLE 列有像“1234”或“1234a”这样的记录时,代码会显示这种记录,但是当它有像“abcd”这样的记录时,它不会显示。
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.原因是 WHERE 子句(查询方法的第三个参数)很可能不是预期的表达式。
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).所以Example列被认为是比较的结果,它要么是假的要么是真的(0 或 1)。
As abcd in non-numeric it is 0 false.作为非数字中的abcd ,它是 0 假。
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.当首先遇到一个数字时,例如123或123ABC,然后 SQlite 会考虑该数值,如果它不是 0,则为真。
False rows (0) are ignored, true rows (non 0) are included and hence the result.假行 (0) 被忽略,真行(非 0)被包括在内,因此结果。
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.然后这将返回在 Example 列中的任何位置具有 a 或 A 的所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.