Home > OS >  delete item from local database function doesn't work
delete item from local database function doesn't work

Time:02-03

//the following function 'listens to the click' - here we are using an 'item click listener' which gives you a number of which one was clicked
        lv_customerList.setOnItemClickListener((parent, view, position, id) -> {
            //getting the customer that was just clicked to send it to the method to delete
            //the parent of the listener is the listview
            CustomerModel clickedCustomer = (CustomerModel) parent.getItemAtPosition(position); //this tells me which person was just clicked
            //now we call the "deleteOne" function from the databaseHelper class
            dataBaseHelper.deleteOne(clickedCustomer);
            //now we update the list view
            ShowCustomersOnListView(dataBaseHelper);
            Toast.makeText(MainActivity.this, "Deleted"   clickedCustomer,Toast.LENGTH_SHORT).show();
        });

This is the code in the MainActivity

//creating a new function that will delete contents from a database
    public boolean deleteOne(CustomerModel customerModel) {
        //find customerModel in the database. if is is found, delete it and return true, otherwise return false

        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "DELETE FROM"   CUSTOMER_TABLE   "WHERE"    CUSTOMER_ID   " = "   customerModel.getId();

        Cursor cursor = db.rawQuery(queryString, null);

        if (cursor.moveToFirst()) {
            return true;
        }
        else {
            return false;
        }
    }

And this is the code from the DatabaseHelper activity

I followed the https://youtu.be/312RhjfetP8 tutorial and copied the code exactly (although the android IDE suggested to change the...

new AdapterView.OnItemClickListener()  

to a lambda (in the method in mainactivity)

But now whenever I click one of the rows (see video to understand what i mean) in the app - the app just crashes and I'm not sure why :(

Could someone please help me out?

Thank you so much!

CodePudding user response:

I'm pretty sure that you miss a lot of space characters in the query String.

String queryString = "DELETE FROM"   CUSTOMER_TABLE   "WHERE"    CUSTOMER_ID   " = "   customerModel.getId();

should be changed to

String queryString = "DELETE FROM "   CUSTOMER_TABLE   " WHERE "    CUSTOMER_ID   " = "   customerModel.getId();

CodePudding user response:

DeleteBuilder<CustomerModel, Integer> deletebuilder = 
                    mcustomermodel.deleteBuilder();
  • Related