2013-08-05 39 views

回答

0

要更新的每一行,你需要的东西是这样的:

void updateMyTableText (MyTableRow row) 
{ 
    ContentValues values = new ContentValues(); 
    values.put ("text", MyTableRow.text); 

    String where = "id = ?"; 
    String[] whereArgs = new String[1]; 
    whereArgs[0] = Long.toString (row.id); 

    getDb().update ("MyTable", values, where, whereArgs); 
} 

其中MyTableRow是

class MyTableRow 
{ 
    long id; 
    String text; 
} 

您还需要一些查询,以获得“文本”的行1和3

这里做一个查询的一种方法:

long getId (String text) 
{ 
    // do the query 
    String query = "select id from MyTable where text = ? "; 
    String[] args = new String[1]; 
    args[0] = text; 
    Cursor cursor = getDb().rawQuery (query, args); 
    if (cursor == null) 
    throw new IllegalStateException ("cursor is null"); 

    try 
    { 
    // get the results 
    if (!cursor.moveToNext()) 
     return -1; // row not found 

    long id = cursor.getLong (0); 
    return id; 
    } 
    finally 
    { 
    cursor.close(); 
    } 
} 
+0

如何从数据库检索id如果我有两个值文本? – doomed

+0

您的解决方案工作!感谢帮助! – doomed

+0

很高兴听到它的作品。我还更新了示例以显示如何执行查询。 –

0

你们是不是要更改行本身的价值?大多数数据库系统不允许您任意交换值,因为假设它们是永久关联的。您可以发出UPDATE命令来永久修改这些值,但暂时这样做可能会成为数据返回后处理的问题。

UPDATE table_name 
SET text=three 
WHERE id=1; 

UPDATE table_name 
SET text=one 
WHERE id=3;