5

这是一个简单的!然而,我错过了一些东西。请帮帮我。在这里,我试图通过 id 获取值,但无法这样做。即使更改了 id 的值,它也会返回相同的值。

    db = openOrCreateDatabase("DBSOURCE", 0, null);
    Cursor cursorc = db.rawQuery("SELECT * FROM LIST WHERE ID="+id+"", null);
    cursorc.moveToFirst();

    int NameID = cursorc.getColumnIndex("Name");
    int mobilenumberID = cursorc.getColumnIndex("MoblieNumber");

    edName.setText(cursorc.getString(NameID));
    edMobNum.setText(cursorc.getString(mobilenumberID));

    cursorc.close();

    db.close();
4

5 回答 5

8

1-或更好地使用参数化语句

String query = "SELECT COUNT(*) FROM " + tableName + " WHERE columnName = ?";
cursor = db.rawQuery(query, new String[] {comment});

2 - 使用 if 条件 c.moveToFirst() 或 c.getCount() >0 或 (!c.isAfterLast())

if (c.moveToFirst()){ 
    do{ 
       //if you not need the loop you can remove that
       id = c.getInt(c.getColumnIndex("_id"));
   }
 while(cursor.moveToNext());
}c.close(); 
于 2012-06-26T11:23:40.807 回答
5

id 列标题实际上是“ID”吗?或者是一个设置为“_id”的变量(Android 数据库中主键的常用列名)?

如果是后者,则您的查询不正确,因为您使用“ID”作为文字列名。尝试将其更改为:

Cursor cursorc = db.rawQuery("SELECT * FROM LIST WHERE " + ID + " = " + id, null); 

甚至这个:

Cursor cursorc = db.rawQuery("SELECT * FROM LIST WHERE _id = " + id, null); 
于 2012-06-26T14:03:44.880 回答
2

尝试使用

Cursor cursorc = db.rawQuery("select * from list where ID = ?", new String[] {id+""});
于 2012-06-26T11:24:51.773 回答
2

试试这种方式

    Suppose long id=5;
    String[] col=new String[]{KEY_ROWID,KEY_NAME,KEY_ADDRESS};  // your column which data u want to retrive if id is same 
    Cursor c=db.query(DATABASE_TABLE, col, KEY_ROWID+"="+id,null, null, null, null);
    if(c!=null){
    c.moveToFirst();
    // get data here which u want accroding to ur requirement 
    }
于 2012-06-26T11:40:08.513 回答
0

尝试这个

Cursor cursorc = db.rawQuery("SELECT * FROM LIST WHERE ID='"+id+"'", null);
于 2012-06-26T11:24:34.647 回答