android的sqlite的比赛没有返回值 [英] android sqlite match returns nothing
问题描述
这是我的code创建一个FTS3表
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 });
返回什么。我甚至不知道如何从SQL错误输出任何错误,如果有任何。顺便在这里就是我所谓的适配器
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.
推荐答案
对于新手来说,这是更好的:
For starters, this is nicer:
for(mCursor.moveToNext()) {
alRecipeID.add(mCursor.getString(1));
}
修改:
while(mCursor.moveToNext()) {
alRecipeID.add(mCursor.getString(1));
}
和你的问题应该是可以解决的,如果你只是使用MATCH子句来过滤选择在全文检索,就像这样:
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'
您必须手动格式化MATCH语句,因为通配符去上班非常不好,是这样的:
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"});
修改,现在用例如code :
经测试,在Android上,它肯定似乎工作就好了。
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();
这篇关于android的sqlite的比赛没有返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!