Delete row in database table given one column value – which is a string

I have a table with column headings: | _id (long) | Name (String) | x (integer) | y (integer) |

I want to delete the row in the table that has Name myName.

  • How can we hide include layout programatically in Android?
  • notifyDataSetChanged() makes the list refresh and scroll jumps back to the top
  • Maintaining a PhoneGap Hybrid/Web App Codebase
  • GPU programming on Android devices
  • How to show ProgressDialog across launching a new Activity?
  • Checkbox unchecked when I scroll listview on Android
  • // In onCreate
    dbHelper = new DBAdapter(this);
    // Function in DBAdapter class
    public boolean deleteTitleGivenName(String myName) 
        return dbHelper.delete(DATABASE_TABLE_2, KEY_NAME + "=" + myName, null) > 0;
    // Function call in java code
    dbHelper.deleteTitleGivenName(myName);  // this is where my code fails  

    Just as a note: myName is for sure in the database. Also, I can not use the ROWID since I am getting myName from a ListView.

    I just started programming with Android and I have tried for days to solve this problem.
    Is my WHERE clause correct (KEY_NAME + “=” + myName)?

    Thanks in advance.

    Related posts:

    How to make a chat application in android?
    unexpected error while parsing input invalid uiautomator hierarchy file
    Android: Hiding the keyboard in an overridden “Done” keypress of EditText
    Track info of currently playing music
    What is the android UI thread stack size limit and how to overcome it?
    How to save a cookie in an Android webview forever?
  • JavaPoet + Android Studio “addModifiers(Modifier) cannot be applied to Modifier”
  • Saving EditText content in RecyclerView
  • Questions about Android application update
  • Android Fragments: When to use hide/show or add/remove/replace?
  • How can I trigger the Google Now action for testing?
  • Failed to install *.apk on device timeout Launch canceled
  • 4 Solutions collect form web for “Delete row in database table given one column value – which is a string”

    Sure, that works, although I would recommend

    dbHelper.delete(DATABASE_TABLE_2, KEY_NAME + "=?", new String[] { myName })

    for safety reasons. That way, you can have special characters without breaking your query. Did this not work?

    Try putting the value of the where clause in quotes…

    KEY_NAME + "='" + myName + "'"

    Also, it is bad practice (generally) to use a string-based WHERE clause. This is because if my name has an apostrophe in it, your app will not work. You should probably look for an alternative way to delete records from a database on Android.

    depending on the complexity of your where condition, the “delete” method will not be the best solution.

    for example, if you want to delete all records whose “_id” is in a second table, like you would write in transactSQL: “…WHERE _id IN (SELECT _id FROM…” then the best solution might be to use the “.execSQL()” method directly on your database.

    myDatabase.execSQL("DELETE FROM myTable WHERE _id IN (SELECT _id FROM myOtherTable)");

    or you could go real ugly and do something like:

    int cursorRows = cursor.getCount();
    String[] id2String = new String[cursorRows];
    String id2StringUnique = "";
    //for (int i=0;i<cursorRows;i++) {
    int i=0;
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){    
        id2String[i] = String.valueOf(cursor.getLong(index_ID));
        if (i==0) {
            id2StringUnique = "'"+id2String[i]+"'";
        } else {
            id2StringUnique += ",'"+id2String[i]+"'";
    dbHelper.delete(DATABASE_TABLE_2, "_id IN (", id2StringUnique +")");

    depending on number of items, you might have a problem with the length / size of your argument – besides it being diselegant to the extreme.

    just try this simple code

    private void deleteItem()
          SQLiteDatabase db = mOpenHelper.getWritableDatabase();
          db.delete(TABLE_NAME, " title = 'haiyang'", null);
        } catch (SQLException e) {
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.