Android 如何交换SQLite列中的值?

Android 如何交换SQLite列中的值?,android,sqlite,Android,Sqlite,我有一个表(id,text),我需要交换如下列中的两个值: Before. 1 one. 2 two. 3 three. After ( 1 and 3 swap) 1 three 2 two 3 one void updateMyTableText (MyTableRow row) { ContentValues values =

我有一个表(id,text),我需要交换如下列中的两个值:

Before. 
           1 one. 
           2 two.
           3 three.

After ( 1 and 3 swap)          
           1 three 
           2 two
           3 one
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);
}

要更新每一行,您需要如下内容:

Before. 
           1 one. 
           2 two.
           3 three.

After ( 1 and 3 swap)          
           1 three 
           2 two
           3 one
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();
  }
}

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

UPDATE table_name
SET text=three
WHERE id=1;

UPDATE table_name
SET text=one
WHERE id=3;

如果我有两个文本值,如何从db中检索id?很高兴听到它能工作。我还更新了示例,以演示如何执行查询。