Home > Blockchain >  Can't delete SQLite Database entry with EditText and Button
Can't delete SQLite Database entry with EditText and Button

Time:11-11

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 :))

CodePudding user response:

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.

CodePudding user response:

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);
        }
  • Related