Android检查行中的类似数据
我试图检查数据库中是否已经存在字段,为此,我在DBAdapter中使用此方法。我不确定这是不是最好的解决办法Android检查行中的类似数据,android,sqlite,Android,Sqlite,我试图检查数据库中是否已经存在字段,为此,我在DBAdapter中使用此方法。我不确定这是不是最好的解决办法 public Cursor getRowByName(String rowId) { String where = KEY_ACT + "=" + rowId; Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
public Cursor getRowByName(String rowId) {
String where = KEY_ACT + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
然后,当我点击我的按钮时,我会:
Cursor data = myDb.getRowByName(item);
if(cursor.moveToFirst()) {
Toast toast = Toast.makeText(getApplicationContext(),"It Exists", Toast.LENGTH_SHORT);
toast.show();
}
else {
Toast toast = Toast.makeText(getApplicationContext(),"It dosent exist", Toast.LENGTH_SHORT);
toast.show();
}
我的'item'是我要检查的字符串。
我不知道我做错了什么,但我对安卓有点陌生,所以如果你们能给我一个提示,我会解释很多:)
谢谢大家! 用于检查来自查询的游标结果中的结果数,如下所示:
Cursor data = myDb.getRowByName(item);
if(cursor.getCount()>0) {
// record found in db
}
else {
// record not found in db
}
使用where和where的正确方法:
db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_ACT + "= ?", new String[]{rowId},
null, null, null, null);
我终于做到了,我按照你们的建议做了,而且很有效 我的DBAdapter方法: 公共游标getRowByName(字符串名称){ } 我是我的班级:
Cursor data = myDb.getRowByName(item);
if(data.moveToFirst()) {
Toast toast = Toast.makeText(getApplicationContext(),"It EXISTS", Toast.LENGTH_SHORT);
toast.show();
}
else {
Toast toast = Toast.makeText(getApplicationContext(),"It dosent", Toast.LENGTH_SHORT);
toast.show();
long newId = myDb.insertRow( timerValue.getText().toString(), item);
Cursor cursor = myDb.getRow(newId);
displayRecordSet(cursor);
}
谢谢你@tachyonflux,我想这是我的问题的一部分:)谢谢你@ρцσρѕρєK的帮助 仍然可以工作,我得到了相同的错误:android.database.sqlite.SQLiteException:near“House”:语法错误(代码1):,在编译时(…)我认为查询可以工作,因为它显示在错误中,但我不知道是什么导致了这个错误,我认为它必须是我的光标,但看起来像。。。编辑:“House”是我想在本例中找到的字符串。我想如果您显示“House”的位置会有所帮助is@DanielFerreira:按
字符串执行,其中=KEY_ACT+“='”+rowId+“”代码>
Cursor data = myDb.getRowByName(item);
if(data.moveToFirst()) {
Toast toast = Toast.makeText(getApplicationContext(),"It EXISTS", Toast.LENGTH_SHORT);
toast.show();
}
else {
Toast toast = Toast.makeText(getApplicationContext(),"It dosent", Toast.LENGTH_SHORT);
toast.show();
long newId = myDb.insertRow( timerValue.getText().toString(), item);
Cursor cursor = myDb.getRow(newId);
displayRecordSet(cursor);
}