如何通过id从DB获取特定值
这是我的表格:TABLE-RECORDS
- (表格名称)和KEY-ID
,KEY-PRICE
...我正试图通过KEY-PRICE
获取KEY-ID
并且可以不。怎么做?
答案 0 :(得分:0)
我不知道这是否正是您所寻找的,但这是查询。
SELECT key-price FROM table-record WHERE key-id='id number you need';
答案 1 :(得分:-1)
//如果我弄错了,请更改数据库的列名
public Cursor getCursor(int id) {
SQLiteDatabase db = this.getReadableDatabase();
String from[] = {"key-price"};//this is the edit1
String where = "key-id=?";//this is the edit2
String[] whereArgs = new String[]{String.valueOf(id)+""}; //this is the edit3
Cursor cursor = db.query(true, table-records, from, where, whereArgs, null, null, null, null);
return cursor;
}
//只需调用此函数即可看到魔法
private int getPrice(int id) {
Cursor c = getCursor(id);
int price=-1;
if(c != null)
{
while(c.moveToNext){
//assuming price is an integer
price = c.getInt(0);//edit 4
// use these strings as you want
}
}
return price;
}