How to delete row in first sqlite position in android

How to delete row in sqlite first position in android Now I use this code to delete 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 the first position. Since KEY_ID will not be the same all the time when the deletion occurs

+4
source share
4 answers

use this method to delete the first line

  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(); } 
+9
source

Write a function that will return you the identifier of the first line. Use this identifier in your delete function.

+2
source

I suggest you get the data that is in the first position. Get the data by moving the cursor to the first position, extract the identifier, and then call the method to remove this identifier from the table. Thus, you always have data that is in the first position. Your request seems to be writing to me.

0
source

Change the way so it looks like this:

 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."); } 

Note that dbHelper is an dbHelper object defined as a member variable.

0
source

Source: https://habr.com/ru/post/1468884/


All Articles