我有一个包含名称字段的 SQLLite 站点数据库。我有保存和创建按钮。我想根据名称字段中的值是否存在来运行我的 addRecord()
或 updateRecord()
。
我的DBAdapter
中有这个方法:
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
如何创建类似的方法来根据提供的名称字符串获取 rowID
?
即
public Cursor getRowID(String _name) {
...
}
最佳答案
在 where 子句中搜索名称而不是 ID。
public Cursor getRowID(String _name) {
String where = "name = '" + _name + "'";
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
从游标中获取 rowID,如下所示:
Cursor c = getRowID("John");
int rowID = c.getInt(c.getColumnIndex(KEY_ROWID));
关于java - 如何检查 SQLite 数据库中是否存在 "name"值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16629287/