简体   繁体   English

查询仅从表中返回数字或数字+字母组合。 如何修改它以显示带字母的记录?

[英]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.当首先遇到一个数字时,例如123123ABC,然后 SQlite 会考虑该数值,如果它不是 0,则为真。

  • 0ABC would be false. 0ABC 将是错误的。
  • -9ABC wold be true. -9ABC 是真的。
  • ABC999 would be false (non-numerics first) ABC999 为假(非数字优先)

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.

相关问题 字母和数字组合后如何不分割 - How NOT to split after a combination of letters and numbers 在数组中搜索字母和数字的组合 - Searching an Array for a combination of letters and numbers 如何计算唯一字符(仅字母和数字) - How to count unique characters (only letters and numbers) Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers 如何动态地将事件处理程序从文本字段中的数字仅更改为字母? - How dynamically changing the event handler from numbers only to letters in textfield? 如何检查字符串是否仅由字母和数字组成 - How to check if a string is made only of letters and numbers 如何使用JFormattedTextField只允许字母和数字 - How to use JFormattedTextField allowing only letters and numbers 正则表达式仅匹配字母和数字 - Regex to match only letters and numbers 如何从文本文件java中读取数字和字母的组合 - How can I read a combination of numbers and letters from a text file java 在Java中,如何检查字符串是否同时包含字母和数字,但仅包含字母和数字? - In Java, how do I check if a string consists of both letters and numbers, but only letters and numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM