gpt4 book ai didi

android sqlite匹配不返回任何内容

转载 作者:行者123 更新时间:2023-11-30 04:19:55 24 4
gpt4 key购买 nike

这是我创建 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));
}

我是不是全文搜索错了?

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

最佳答案

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

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

关于android sqlite匹配不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9428069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com