imin
imin

Reputation: 4578

android sqlite match returns nothing

this is my code to create an fts3 table

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

now if i queried it using something like this

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

it works, i got the returned data, but when I tried to search using full text like this

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  });

it returns nothing. i don't even know how to output any error from the sql error if there's any. by the way here's how I called the adapter

mCursor = dbAdapter.searchRecipe(searchString);

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

did i do the full text search wrong?

note: the adapter above returns data if i didn't use the 'match' query to do full text search.

Upvotes: 1

Views: 2054

Answers (1)

Jens
Jens

Reputation: 17087

For starters, this is nicer:

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

Edit:

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

And your problem should be fixable if you just use the MATCH clause to filter your selection in full-text search, like this:

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

You'll have to manually format the MATCH-statement, since wildcards are going to work extraordinarily bad, something like this:

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

Edit, now with example code: Tested on Android, and it sure seems to work just fine.

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();

Upvotes: 1

Related Questions