Deleting Row in SQLite in Android

Deleting Row in SQLite in Android

You can try like this:

 //---deletes a particular title---
public boolean deleteTitle(String name)
{
return db.delete(DATABASE_TABLE, KEY_NAME + "=" + name, null) > 0;
}

or

public boolean deleteTitle(String name) 
{
return db.delete(DATABASE_TABLE, KEY_NAME + "=?", new String[]{name}) > 0;
}

How to delete a single row in android sqlite

You can use SQL DELETE statement to choose which entry you want to delete.
When using a string value as the selector, make sure you enclose the value in ''.

    /**
* Remove a contact from database by title
*
* @param title to remove
*/
public void removeSingleContact(String title) {
//Open the database
SQLiteDatabase database = this.getWritableDatabase();

//Execute sql query to remove from database
//NOTE: When removing by String in SQL, value must be enclosed with ''
database.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + CONTACTS_COLUMN_TITLE + "= '" + title + "'");

//Close the database
database.close();
}

Then whenever you want to remove an entry from the database use the following:

DBHelper dbHelper = new DBHelper(context);
dbHelper.removeSingleContact("CONTACT_TO_REMOVE_HERE");

UPDATE
Assuming that your ListView dataset is an ArrayList containing ContactListItems, you can set an OnItemClickListener for your ListView and use it to get your selected title, and then remove it.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyCart.this);
alertDialog.setCancelable(false);
alertDialog.setMessage("Delete item?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String title;

title = arrayList.get(position).getTitle();

dbHelper.removeSingleContact(title);

//Update your ArrayList
arrayList = dbHelper.getAllContacts();

//Notify your ListView adapter
adapter.notifyDataSetChanged();

}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();

}

});

Deleting row in Sqlite table

Try this

db.delete(TABLE_ROUTE_NOTE, COLUMN_ROUTE_NOTE_POSITION + " = " + notePosition, null)

Delete a row from sqlite database inside a recyclerview

You have to write onClick event on viewholder class. In your case "ViewHolderMain". you have to put

itemView.setClickable(true);

on ViewHolderMain constructor. then you can simply write the on click event.

ImageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Your delete method
}
});

After deleting item from sqlite u must add

notifyDataSetChanged();

to delete the item from view as well ..

Deleting Only One Row in SQLite Using ContentProvider

Thanks to everyone. I was able to solve the problem. The problem was that I'm passing the row's position in the list view considering it to be the same as row's _id in the SQLite database. This is true only when we first create the listview rows and store them in the database because they match. However, when we delete rows, the listview rows' positions change their values and no longer valid to be passed as item's _id in sqlite in the selection criteria because sqlite item's _id stays the same and isn't affected by the position changes in the listview. To solve this I needed to pass a cursor of the clicked item and extract the item's _id from the cursor.
For simplicity, let's say I want an item to be deleted when clicked:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long id) {

Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);

Somewhere in the category fragment I'm declaring constants for category columns:

// These indices are tied to CATEGORY_COLUMNS.  If FORECAST_COLUMNS changes, these
// must change.
static final int COL_CATEGORY_ID = 0;

Then I can pass that cursor anywhere to extract the _id of the item you want to delete:

@Override
public void deleteItem(Uri contentURI, Cursor cursor) {

long id = cursor.getLong(CategoryFragment.COL_CATEGORY_ID);

// Delete items of a category
String[] selctionArg = {String.valueOf(id)};

// Delete category
getContentResolver().delete(
MenuContract.CategoryEntry.CONTENT_URI,
MenuContract.CategoryEntry._ID + "=?",
selctionArg
);
}


Related Topics



Leave a reply



Submit