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
In order to delete a particular row from a table in SQL, we use the DELETE query, The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multiple records depending on the condition we specify in the WHERE clause.
The TRUNCATE TABLE statement is used to remove all records from a table. SQLite does not have an explicit TRUNCATE TABLE command like other databases. Instead, it has added a TRUNCATE optimizer to the DELETE statement. To truncate a table in SQLite, you just need to execute a DELETE statement without a WHERE clause.
If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.
use this method to delete first row
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With