skip to Main Content

I’m trying to implement a method, so I can enter a value (integer) into an edittext field and click a button to delete an SQLite Database entry. I can’t seem to get it to work though.

Here is the method in the DatabaseManager class:

public void deleteEntry(Integer entryid){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete("data", "entryid" + "="" + entryid + """, null);
}

Here is the declaration in the activity class:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    _btnDelete = (Button) findViewById(R.id.btnDelete);
    _etDelete = (EditText) findViewById(R.id.etDelete);
    db = new DatabaseManager(this);

    _btnDelete.setOnClickListener(this);
    
    }

And here is the method I’m calling in the Activity, when the delete button is clicked:

@Override
public void onClick(View v) {
[...]
else if (v.equals(_btnDelete)){
            Integer val = Integer.parseInt(_etDelete.getText().toString());
            db.deleteEntry(val);
        }

And here is the layout for the EditText and Button:

            <EditText
                android:id="@+id/etDelete"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:digits="0123456789"
                android:inputType="number"
                android:hint="Enter ID to delete"/>

            <Button
                android:id="@+id/btnDelete"
                android:layout_width="180dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:text="Delete"/>

And lastly, the Database scheme:

public void onCreate(SQLiteDatabase db){

    try{
        db.execSQL("CREATE TABLE data (" +
                "entryid INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT," +
                "weight INTEGER," +
                "reps INTEGER," +
                "entrytime TEXT)"
        );

What am I doing wrong? Thank you in advance :))

3

Answers


  1. Try using :-

    db.delete("data", "entryid=?", new String[]{String.valueOf(entryid)});
    

    Re comment:-

    Unfortunately that didn’t change anything, ….

    I believe that you also need to change :-

    else if (v.equals(_btnDelete)){
                Integer val = Integer.parseInt(_etDelete.getText().toString());
                db.deleteEntry(val);
            }
    

    to

    else if (view.getId() == R.id.btnDelete){
                Integer val = Integer.parseInt(_etDelete.getText().toString());
                db.deleteEntry(val);
            }
    
    Login or Signup to reply.
  2. Try this:

    public void deleteEntry(Integer entryid){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("data", "entryid" + "='" + entryid + "'", null);
    }
    

    Although " is more recently supported, it is better for to denote SQL strings.

    Login or Signup to reply.
  3. try this it may help you

    public void removeSingleItem(String id) {
            SQLiteDatabase database = this.getWritableDatabase();
    
            long result = database.delete(TableName, "id=?", new String[]{id});
            if (result == -1) {
                Toast.makeText(context, "Item Not Deleted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Item Deleted Successfully", Toast.LENGTH_SHORT).show();
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search