我使用此代码检查表格中是否存在行:
public boolean checkIdiomInFavorite(Idiom idiom){
String query = "SELECT *" + " from " + IdiomsOpenHelper.TABLE_FAVORITE +
" where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";
Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });
if(cursor.moveToNext()){
return true;
}
else{
return false;
}
}
但我总是得到false
有什么想法吗?
答案 0 :(得分:1)
我替换了这个
if(cursor.moveToNext()){
return true;
}
else{
return false;
}
与
return(cursor.moveToFirst());
答案 1 :(得分:1)
更改此查询字符串...
String query = "SELECT *" + " from " + IdiomsOpenHelper.TABLE_FAVORITE +
" where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";
...到...
String query = "SELECT * from " + IdiomsOpenHelper.TABLE_FAVORITE +
" where " + IdiomsOpenHelper.COLUMN_FAV_ID = " + idiom.getId();
然后改变这一行......
Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });
...到...
Cursor cursor = database.rawQuery(query, null);
答案 2 :(得分:0)
使用以下代码:
public boolean checkIdiomInFavorite(Idiom idiom){
String query = "SELECT *" + " from " + IdiomsOpenHelper.TABLE_FAVORITE +
" where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";
Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });
if(cursor.moveToNext()){
return true;
}
else{
return false;
}
}
你可以检测到这种尊重吗?我正在使用SELECT *,这意味着所有列...或者使用您在该表中实际拥有的任何列名。
您还可以计算表的总行数以检查它是否返回0 ...如果返回0则不存在数据。