如何通过id从DB获取特定值

时间:2017-12-03 13:07:18

标签: android mysql sql database sqlite

如何通过id从DB获取特定值 这是我的表格:TABLE-RECORDS - (表格名称)和KEY-IDKEY-PRICE ...我正试图通过KEY-PRICE获取KEY-ID并且可以不。怎么做?

2 个答案:

答案 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;
}