1

I have a SQLite DB with a table called "students". In the students table, there are 2 columns: "student_id" and "student_name".

I am trying to delete a row from an SQLite table using this code:

String TABLE_STUDENTS = "students";
String COLUMN_STUDENTNAME= "student_name";

public void deleteUser(String name) {
    database.delete(TABLE_STUDENTS, COLUMN_STUDENTNAME + "=" + name, null);
}

However, when I try to delete the user "John" I get this error message from LogCat

12-19 16:28:47.132: E/SQLiteLog(14883): (1) no such column: John

I was trying to follow the example here: Deleting Row in SQLite in Android, but I could swear my syntax is the same.

Guessing I am doing something stupid, but anyone able to help? Thanks.

Community
  • 1
  • 1
jhnewkirk
  • 87
  • 9
  • The answer below works great. The other way that works which is what I was going for was `database.delete(TABLE_STUDENTS, COLUMN_STUDENTNAME + "=\"" + name + "\"", null);` I forgot to put quotes around it. Thanks to everyone. – jhnewkirk Dec 19 '13 at 21:47
  • That's something you should not do: http://bobby-tables.com/ (There are people out there that have an `'` in their name and that will break your code) – zapl Dec 19 '13 at 21:49

3 Answers3

4

You may try:

database.delete(TABLE_STUDENTS,COLUMN_STUDENTNAME +" = ?", new String[] { name });
MillaresRoo
  • 3,808
  • 1
  • 31
  • 37
1

What you are doing is sending the equivalent statement DELETE FROM students WHERE student_name=John, which is why it is looking for a column called John. You need to use the third parameter of the method to provide the argument, so:

final String[] deleteConditions = new String[]{ name };
database.delete(TABLE_STUDENTS, COLUMN_STUDENTNAME + "=?", deleteConditions);

Note that you can delete multiple rows by adding more entries to the array deleteConditions.

  • "Note that you can delete multiple rows by adding more entries to the array deleteConditions" - please explain, in this form it's misleading. – laalto Dec 20 '13 at 07:14
1

That works, although I would recommend

db.delete(TABLE, "column_name=?", new String[] { String.valueOf(custname) });

or use

String query = "DELETE FROM " +TABLE_NAME+ " WHERE "  + COLUM_NAME+ " = " + "'"+VALUE +"'" ;
db.execSQL(query);
M D
  • 47,665
  • 9
  • 93
  • 114