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

时间:2022-12-21 11:08:47

How to get specific value from DB by id.
This is my table: TABLE-RECORDS-(name of table) and KEY-ID , KEY-PRICE ... I'm trying to get KEY-PRICE by KEY-ID and can not. How to do it?

如何通过id从DB获取特定值。这是我的表:TABLE-RECORDS-(表的名称)和KEY-ID,KEY-PRICE ...我正在尝试通过KEY-ID获取KEY-PRICE而不能。怎么做?

2 个解决方案

#1


-1  

// please change the column names of database if i have mistaken

//如果我弄错了,请更改数据库的列名

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

//just call this function and see the magic

//只需调用此函数即可看到魔法

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

#2


0  

I don't know if this is exactly what you are looking for, but this is the query.

我不知道这是否正是您正在寻找的,但这是查询。

SELECT key-price FROM table-record WHERE key-id='id number you need';

#1


-1  

// please change the column names of database if i have mistaken

//如果我弄错了,请更改数据库的列名

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

//just call this function and see the magic

//只需调用此函数即可看到魔法

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

#2


0  

I don't know if this is exactly what you are looking for, but this is the query.

我不知道这是否正是您正在寻找的,但这是查询。

SELECT key-price FROM table-record WHERE key-id='id number you need';