如何构建查询以检查从多个行构建的字符串是否出现多次?

时间:2012-12-05 23:41:26

标签: android arrays sqlite

有点难以解释,但我们走了:

我有一个数据库表,看起来有点像这样(为了这个问题的目的非常简化);

_id | area_link | text1 | text2 | text3 | text4
1          1      this     is       one    example
2          1      this     is     another  example
3          2      this     is       one    example
4          2      this     is     another  example
5          2      this     is     another  example

所以我可以构建一个查询来查找数据WHERE area_link等于2,但我想以某种方式创建一个如下所示的数组:

thisisoneexample
thisisanotherexample

但这不会导致这种情况(这是我对简单查询的看法):

thisisoneexample
thisisanotherexample
thisisanotherexample

以下是我在数据库助手类中实际查询的示例:

public Cursor fetchComponentSpecForArea(long areaId, String componentType, String specSaved) throws SQLException {
    Cursor mCursor =
            rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
                    AREA_LINK,
                    COMPONENT_TYPE,
                    MANUFACTURER,
                    TEXT1,
                    TEXT2,
                    TEXT3,
                    TEXT4,
                    NOTES_SPEC,
                    SPEC_SAVED}, 
                    AREA_LINK + " = " + areaId + " AND " + COMPONENT_TYPE + " = ? AND " + SPEC_SAVED + " = ?", 
                    new String[] {componentType, specSaved}, 
                    null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

我在主代码中如何调用它的示例:

        final Cursor componentSpecForAreaCursor = (Cursor) rmDbHelper.fetchComponentSpecForArea(areaId, componentType, specSaved);
        if (componentSpecForAreaCursor.moveToFirst()){
            String manufacturer = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.MANUFACTURER)), "");
            String text1 = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.TEXT1)), "");
            String text2 = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.TEXT2)), "");
            String text3 = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.TEXT3)), "");
            String text4 = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.TEXT4)), "");
            String notes_spec = RMUtilities.notEmpty(componentSpecForAreaCursor.getString(componentSpecForAreaCursor.getColumnIndex(RMDbAdapter.NOTES_SPEC)), "");
            rmDbHelper.saveDamagedComponentSpec(componentId, manufacturer, text1, text2, text3, text4, notes_spec);
        }

所以我想我必须:

create an array,
query the database,
if cursor > 0 then go to first and:
build a string from the relevant database fields,
put this into array
build next string from the relevant database fields,
check if this matches any of entries in array if not:
put this into array
等等,但可以帮忙!!提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用Pattern和Matcher对象。然后调用匹配器对象的.find()方法并执行您想要的操作!

以下是官方文档中可以为您提供帮助的链接:http://developer.android.com/reference/java/util/regex/Pattern.html