Aldrin Ramirez
Aldrin Ramirez

Reputation: 129

Android: Cant delete item in Database

Whenever I LongPress an item to delete it in the database, it crashes. Here's my code

public class MyListViewLongClickListener implements OnItemLongClickListener {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) {
        String itemInTheRow = adapter.getItem(position).toString();
        String[] details = itemInTheRow.split(" - ");

        final String name = details[0];
        String brand = details[1];
        Integer qty = Integer.parseInt(details[3]);
        Double size = Double.parseDouble(details[2]);
        Double price = Double.parseDouble(details[4]);
        AlertDialog.Builder confirmDialog = new AlertDialog.Builder(DeleteActivity.this);
        confirmDialog.setTitle("Confirm Deletion");
        confirmDialog.setMessage("Are you sure you want to \nDelete the record of " + name + "?");
        confirmDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dbTools.deleteShoes(name);
                loadData();
            }
        });
        confirmDialog.setNegativeButton("Cancel", null);
        confirmDialog.show();
        return false;
    }
}

and this is my method in delete

private String tblname = "dtbShoes1";
private String fld_Name = "Name";

public boolean deleteShoes(String name) {
        String deleteQuery = "DELETE FROM " + tblname +
                " WHERE " + fld_Name + " = " + name;

        openConnection();
        db.execSQL(deleteQuery);
        closeConnection();
        return true;
    }

and this is my logcat

10-12 15:50:55.052    1572-1572/com.example.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: bbb (code 1): , while compiling: DELETE FROM dtbShoes1 WHERE Name = bbb
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
            at com.example.finalproject.DatabaseShoes.deleteShoes(DatabaseShoes.java:101)
            at com.example.finalproject.DeleteActivity$MyListViewLongClickListener$1.onClick(DeleteActivity.java:96)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Whenever I change

String deleteQuery = "DELETE FROM " + tblname +
                " WHERE " + fld_Name + " = " + name;

to

String deleteQuery = "DELETE FROM " + tblname +
                " WHERE " + fld_Name + " = " + "name";

it deleted all my record in my database. Any help will be appreciated.

Upvotes: 0

Views: 78

Answers (2)

Shvet
Shvet

Reputation: 1201

This is Delete Method i used in my Application to delete on item at a time:

public void delete_cartitem(int id) {

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, KEY_ID + "=?",
            new String[] { String.valueOf(id) });
    db.close();
}

You can change according to your requirement.

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14408

You missed single quote around column name,So change

String deleteQuery = "DELETE FROM " + tblname +
                " WHERE " + fld_Name + " = " + name;

to

String deleteQuery = "DELETE FROM " + tblname +
                " WHERE " + fld_Name + " ='" + name + "'";

and recommeded way is to use parameterizes query as

 db.delete(tblname, fld_Name + " = ?", new String[] { name });

Upvotes: 3

Related Questions