I am trying to develop a mobile application to get familiar with SQLite database. In here create, add methods are working. But when I am going to delete in list view the mobile app crashes. Here is the code that I have written.
In MainActivity.java
lv_customerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CustomerModel clickedCustomer = (CustomerModel) parent.getItemAtPosition(position);
dataBaseHelper.deleteOne(clickedCustomer);
extracted(dataBaseHelper);
Toast.makeText(MainActivity.this, "Deleted" ,Toast.LENGTH_SHORT).show();
}
});
}
private void extracted(DataBaseHelper dataBaseHelper) {
customerArrayAdapter = new ArrayAdapter<CustomerModel>(MainActivity.this, android.R.layout.simple_list_item_1, dataBaseHelper.getEveryone());
lv_customerList.setAdapter(customerArrayAdapter);
}
}
In databaseHelper.java class
`
public boolean deleteOne(CustomerModel customerModel){
SQLiteDatabase db = this.getWritableDatabase();
String queryString = "DELETE FROM" + CUSTOMER_TABLE + " WHERE " + COLUMN_ID + " = " + customerModel.getId();
Cursor cursor = db.rawQuery(queryString, null);
if(cursor.moveToFirst()){
return true;
}else {
return false;
}
}
You have omitted a space between the keyword
FROMand the table name and thus will encounter an exception along the lines of:-HOWEVER, you would then ALWAYS encounter a returned result of false from the
DeleteOnemethod (see Demo below).This is due to a two-fold issue a) that the
rawQuerymethod should ONLY be used for returning data that is extracted as an output SQL statement i.e. a SELECT statement or some PRAGMA statements and b) that other statements such as DELETE do not return anything and actually result in a rollback undoing what they have done.Instead you should either use:-
an
execSQLmethod, which again does not return a result,exeSQLdoes what is to be done, which may be nothing, or it fails, orthe respective convenience method, e.g. the
deletemethod for a deletion.the convenience methods, in addition to undertaking the action, do return an appropriate result:-
insertmethod returns the rowid or alias thereof for ROWID tablesdeleteandupdatemethods return the number of affected rows (number of rows delete or updated).i.e. the convenience methods invoke the appropriate SQLite API function AFTER the execution to return the value
Demo
The following is a demonstration of:-
execSQLmethod and then returning a useful result, anddeleteconvenience methodThe following is the DatabaseHelper class used for the Demo that is based upon what can be ascertained from your code:-
deleteOnemethoddeleteByExecSQLmethod, which uses the execSQL method to undertake the deletion but also returns the number of rows that have been deleted by interrogating the number of rows in the table before and after the actual deletion and calculating the difference.deleteViaConvenienceMethodTo actually demonstrate the 3 approaches, the following Activity code:-
When run then the results are:-
deletemethod is simpler and more efficient (it uses the the appropriate SQLite interface) and does not rescan the table to get the countIt is suggested that you refer to https://developer.android.com/studio/debug, this would have enabled you to ascertain the initial issue.