tags:

views:

36

answers:

2

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.

// In onCreate
dbHelper = new DBAdapter(this);
dbHelper.open()

// 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  

dbHelper.close();           

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.

A: 

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.

dacris
cool. This code has also solved my problem. For future reference, can you please explain why this works?And thanks a lot for your help!
Yasir Malang
SQL needs quotes (or apostrophies) for strings, just like most other languages, so it knows where the string ends and where the rest of the query continues. It's just like saying if (a.compare("My )string")) rather than (a.compare(My )String)).
EboMike
Btw, string-based clauses aren't that bad - sometimes you just have to do them, and SQL is definitely a better choice than doing it in code. However, I would strongly suggest using "?" (see my answer) rather than putting it directly into the query - as explained above.
EboMike
+2  A: 

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?

EboMike
wow. I tried your code and it works like a charm! Can you please explain the WHERE clause and the WhereArgs string[] ? I know the code works, but I would like to know how. By the way, thanks a lot!
Yasir Malang
"?" is basically a placeholder, so you can do something like "A=?, B<?, C>?". The next argument is a string array that contains all those placeholders, so in this example, you need to have exactly three arguments: new String[] { argumentForA, argumentForB, argumentForC }. The beauty is that those strings can have quotes, apostrophies, and what not, and it'll always work.That's also a good hacker protection, when people try to add SQL into names that are fed into queries.(Btw, not to be pushy - if my answer worked for you, please consider marking it 'best answer' so we both get a better score!)
EboMike
Wow. thanks so much - really good explanation. I tried to Google my question but couldn't solve my problem for like 3 days! I just discovered this forum (so thanks for the 'best answer' tip :)Thanks a lot, and I will be sure to post up more questions in the future.
Yasir Malang