android sqlite match什么都不返回

时间:2012-02-24 09:07:55

标签: android sqlite

这是我创建fts3表的代码

db.execSQL("CREATE VIRTUAL TABLE [videorecipes] USING FTS3 (" + "[recipeID] TEXT, " + "[recipeName] TEXT, " + "[video] TEXT, " + "[thumbs"] TEXT, " + "[ownerID"] TEXT, " + "[chefID"] TEXT, ");");

现在,如果我使用类似的东西查询它

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID, recipeName, thumbs from videorecipes where ownerID = ? AND chefID = ?", new String[] { ownerID, chefID });

它有效,我得到了返回的数据,但当我尝试使用像这样的全文搜索时

Cursor c = sqlDatabase.rawQuery("select docid as _id, recipeID, recipeName, thumbs  from videorecipes  where recipeName MATCH ? AND ownerID = ? and chefID = ?, new String[] { searchRecipeName, ownerID, chefID  });

它什么都不返回。如果有的话,我甚至不知道如何输出sql错误中的任何错误。顺便说一句,这就是我调用适配器的方式

mCursor = dbAdapter.searchRecipe(searchString);

for(mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast()) {
            alRecipeID.add(mCursor.getString(1));
}

我做错了全文搜索吗?

注意:如果我没有使用'match'查询进行全文搜索,上面的适配器会返回数据。

1 个答案:

答案 0 :(得分:1)

对于初学者来说,这更好:

for(mCursor.moveToNext()) {
   alRecipeID.add(mCursor.getString(1));
}

修改

while(mCursor.moveToNext()) {
   alRecipeID.add(mCursor.getString(1));
}

如果您只是使用MATCH子句来过滤您在全文搜索中的选择,那么您的问题应该是可以解决的,例如:

SELECT 
    docid as _id, 
    recipeID, 
    recipeName, 
    thumbs 
FROM 
    videorecipes 
WHERE 
    videorecipes 
MATCH 
    'recipeName:Yourtextgoeshere ownerID:1234 chefID:2345'

你必须手动格式化MATCH语句,因为通配符的工作方式会非常糟糕,如下所示:

sqlDatabase.rawQuery("SELECT docid as _id, recipeID, recipeName, thumbs" +
    " FROM videorecipes WHERE videorecipes MATCH ?", 
    new String[]{"recipeName:Yourtextgoeshere ownerID:1234 chefID:2345"}); 

编辑,现在使用示例代码: 在Android上测试过,它确实可以正常工作。

SQLiteOpenHelper helper = new SQLiteOpenHelper(this, "fts3.db", null, 1) {
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE VIRTUAL TABLE [videorecipes] USING FTS3 (" + "[recipeID] TEXT, "
                + "[recipeName] TEXT, " + "[video] TEXT, " + "[thumbs] TEXT, "
                + "[ownerID] TEXT, " + "[chefID] TEXT)");
    }
};

SQLiteDatabase db = helper.getWritableDatabase();
// Put something in the db
ContentValues values = new ContentValues();
for (int i = 0; i < 20; i++) {
    values.put("recipeID", i);
    values.put("recipeName", "Recipe #" + i);
    values.put("video", "Video" + i + ".avi");
    values.put("thumbs", "thumb" + i + ".jpg");
    // 10 for the ownerID 148 and 10 for the ownerID 1481
    values.put("ownerID", i < 10 ? "148" : "1481");
    values.put("chefID", i);
    db.insert("videorecipes", "chefID", values);
}

Cursor c = db.rawQuery("SELECT docid as _id, recipeID, recipeName, thumbs, ownerID " +
        "FROM videorecipes WHERE videorecipes MATCH ?" ,
        new String[]{"ownerID:1481 recipeName:Recipe"});

int count = c.getColumnCount();
while (c.moveToNext()) {
    for (int i = 0; i < count; i++) {
        System.out.println(c.getColumnName(i) + "=" + c.getString(i));
    }
    System.out.println("-----------------------------");
}
db.close();