【问题标题】:How to get the value of a specific table field by knowing the id and the column?(SQLite)如何通过知道id和column来获取特定表字段的值?(SQLite)
【发布时间】:2018-07-14 14:15:00
【问题描述】:

我尝试通过查找_id 从我的数据库中读取值。

private static  final String DATABASE_NAME = "database.db";
private static final String TABLE_NAME = "database";
private static final String COL1 = "_id";
private static final String COL2 = "Name";

    public String getData(int id, String col){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT value FROM " + TABLE_NAME +" WHERE
 /* id = _id and col = COL2 */", null);
    }

【问题讨论】:

    标签: java android sql database sqlite


    【解决方案1】:

    您必须稍微更改查询。

    "SELECT * FROM " + TABLE_NAME + " WHERE " + COL1 + "=" + id
    

    用你传递给方法rawQuery()的字符串替换它

    上述解决方案可行,但这可能不是使用rawQuery() 方法的最佳方式,因为我们没有使用它的第二个参数selectionArgs。详情请参考official documentation

    所以最终的解决方案看起来像这样:

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COL1 + "=?", new String[] { String.valueOf(id) });
    

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      相关资源
      最近更新 更多