Java 正确的rawQuery
我尝试用Java 正确的rawQuery,java,android,sqlite,Java,Android,Sqlite,我尝试用id=x查询并获取一个数据 我的方法是: public Cursor getDataWithId(String id) { SQLiteDatabase db = this.getWritableDatabase(); Cursor res = db.rawQuery("select * from urun_table where ID = "+id,null); return res; } 我做错了吗?因为我想,我没有正确理解selectionArgs 谢谢。试试这个 p
id=x
查询并获取一个数据
我的方法是:
public Cursor getDataWithId(String id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from urun_table where ID = "+id,null);
return res;
}
我做错了吗?因为我想,我没有正确理解selectionArgs
谢谢。试试这个
public Cursor getDataWithId(String id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from urun_table where ID = '" + id + "'", null);
return res;
}
您可以使用以下内容:
String TABLE_URUN = "urun_table";
String KEY_ID = "ID";
public Cursor getDataWithId(String id) {
SQLiteDatabase db = this.getWritableDatabase();
String query = String.format("SELECT * FROM %s WHERE %s = ?", TABLE_URUN, KEY_ID);
Cursor res = db.rawQuery(query, new String[]{id});
return res;
}
如:
selectionArgsString:您可以在查询的where子句中包含?s,
将由SelectionAgs中的值替换。价值观
将被绑定为字符串
因此,如果您想对以下内容使用多个选项:
SELECT * FROM TABLE WHERE KEY_ID = id AND KEY_OTHER = otherValue
您只需在SelectionAgs参数中添加多个“?”和字符串:
String KEY_OTHER = "other";
public Cursor getDataWithId(String id, String other) {
String query = String.format("SELECT * FROM %s WHERE %s = ? AND %s = ?", TABLE_URUN, KEY_ID, KEY_OTHER);
Cursor res = db.rawQuery(query, new String[]{id, other});
return res;
}
表中的实际ID值是数字还是字符串?