java - 如何检查 SQLite 数据库中是否存在 "name"值?

标签 java android sqlite

我有一个包含名称字段的 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/

相关文章:

java - 使用 LocationTextLocationStrategy 的 iText "Coordinate outside allowed range"异常

java - 如何在 Mac OS X 上安装 mod_jk

java - 在 Jenkins 上运行 Selenium 测试

sqlite - Next.js SSR 与本地 SQLite : "unable to open database file"

java - 以另一种形式使用 Java DB 连接

java - Maven webapp 原型(prototype)项目中设置的欢迎页面在哪里

android - 手电筒在条形码扫描器程序 (Zxing) 中不起作用

android - Gradle、Robolectric 和 Espresso

android - notificationManager 通过 Id 获取通知

java - 安卓| SQLite : Pre-created database updating without using SQLiteOpenHelper