5

How to delete a row in first position the sqlite in android Now i am using this code for delete the row

public void delete(String id)
{
    SQLiteDatabase db = this.getReadableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[]{id});
   // db.execSQL(TABLE_CONTACTS , " ORDER BY " + KEY_ID + " ASC;");
    db.close();
}

But i want to delete the row in first position. Because KEY_ID will not be the same all the time when deletion occur

4

4 回答 4

10

使用此方法删除第一行

    public void deleteFirstRow()
{
    db.open();
    Cursor cursor = db.query(TABLE_CONTACTS, null, null, null, null, null, null); 

        if(cursor.moveToFirst()) {
            String rowId = cursor.getString(cursor.getColumnIndex(KEY_ID)); 

            db.delete(TABLE_CONTACTS, KEY_ID + "=?",  new String[]{rowId});
           }
db.close();
    }   
于 2013-03-13T05:09:47.227 回答
2

编写一个函数,它将返回第一行的 id。在您的删除功能中使用此 ID。

于 2013-03-13T04:51:05.613 回答
0

将您的方法更改为如下所示:

public void deleteFirstRow(String id) {
    int result;
    SQLiteDatabase db = dbHelper.getWritableDatabase();


    // Find all records stored in the table in which you want to delete
    // first row
    Cursor mCursor = database.query(TABLE_CONTACTS, null, null, null, null,
            null, null);

    if (mCursor.moveToFirst()) {
        result = db.delete(TABLE_CONTACTS, KEY_ID + "=?",
                mCursor.getString(mCursor
                    .getColumnIndex(KEY_ID)));

    }

    Log.i("deleteFirstRow", "Deleted " + result + " rows.");

}

注意dbHelperSQLiteOpenHelper定义为成员变量的对象。

于 2013-03-13T05:05:32.283 回答
0

我会建议你得到第一个位置的数据。将光标中的数据移动到第一个位置提取 id,然后调用您的方法从表中删除该 id。通过这种方式,您始终拥有位于第一位的数据。你的查询似乎写信给我。

于 2013-03-13T04:44:27.460 回答