0

有点难以解释,但我们开始吧:

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

_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

等,但可以做一些帮助!提前致谢。

4

1 回答 1

0

尝试使用 Pattern 和 Matcher 对象。然后调用.find()matcher对象的方法,执行你想要的动作!

这是官方文档的链接,可以帮助您:http: //developer.android.com/reference/java/util/regex/Pattern.html

于 2012-12-05T23:56:39.633 回答