1

I'm new to android ... i have developed a Database application , were i'm able to insert the textfield details , but i want to delete the specific row , i need to search by name and delete the user.

can someone help me on this :

My DB code is:

public void deleteRow(String firstname)
{

    try {db.delete(TABLE_NAME,TABLE_ROW_ONE + "=?" + new String[]{firstname},null);}
    catch (Exception e)
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }
}

Activty code is :

deleteButton.setOnClickListener ( new View.OnClickListener() { @Override public void onClick(View v) {deleteRow();} } );

private void deleteRow() { try {

        db.deleteRow(textFieldOne.getText().toString());


        updateTable();


        emptyFormFields();
    }
    catch (Exception e)
    {
        Log.e("Delete Error", e.toString());
        e.printStackTrace();
    }
}

textFieldOne is the first name of the user.

can someone help me on this ..

Ruan_Lopes
  • 1,381
  • 13
  • 18

2 Answers2

0

Remove the null Argument from your delete method... do like this ---->

db.delete(TABLE_NAME,TABLE_ROW_ONE + "=?" + new String[]{ firstname });

It will work fine...

Rocker
  • 669
  • 5
  • 15
0

I think you've misunderstood the purpose of ? in Android+sqlite.

the ? are used as a placeholder which are then replaced with the strings you pass as the third argument to delete and other similar database functions on Android.

In your code you're setting the selection argument a combination of the selection string and the selection arguments string array.

Changing the code from

/* ... */ TABLE_ROW_ONE + "=?" + new String[]{firstname},null);

to

/* ... */ TABLE_ROW_ONE + "=?", new String[]{firstname});

should do the trick.

You can also just say

db.delete(TABLE_NAME, TABLE_ROW_ONE + "='" + firstname + "'", null);

if you want to but the purpose of the ? is to do some kind of caching with the queries so they'd be faster next time.

Zharf
  • 2,638
  • 25
  • 26
  • hey thanks a lot guys ,, but nothing worked for me .. public void deleteRow(String firstname) { // ask the database manager to delete the row of given id try { db.delete(TABLE_NAME,TABLE_ROW_ONE + "=" + firstname,null); } catch (Exception e) { Log.e("DB ERROR", e.toString()); e.printStackTrace(); } } – Sai Krishna Sep 18 '11 at 04:37
  • when i delete it by row ID it gets deleted , but i want to search by name and delete ... but this is not happening .. – Sai Krishna Sep 18 '11 at 04:38
  • double check that TABLE_ROW_ONE actually equals the column name you want to match, and that firstname is actually in the table and firstname is not null... if firstname is null the second argument should be TABLE_ROW_ONE + " is not null". – Zharf Sep 18 '11 at 06:49
  • Can u please take a look at the code and tel me where i'm going wrong... http://www.4shared.com/file/c9zmraI4/AABDatabase_1.html – Sai Krishna Sep 18 '11 at 08:13
  • Another thing that comes to mind is if the firstname string has spaces in them then the last option I gave probably doesn't work correctly, you should use the one with the question mark. – Zharf Sep 18 '11 at 09:11
  • i have tried all he ways .. i'm mising something simple , which i'm unable to fingure out .. can u pls take a look at the code which i have attached in the above link .. it would be great ,, – Sai Krishna Sep 18 '11 at 09:47
  • First off, you're giving the function the string from the wrong text field, double check that... next, you really should use the question mark version (or add '' around the string in the query (my bad, will update answer...) – Zharf Sep 18 '11 at 13:01
  • Thanks a lot Zharf ... i will add the question mark on it .. textfield which i have given is to retrieve from the first name .. – Sai Krishna Sep 18 '11 at 16:12
  • Thanks a lot Zharf ... i will add the question mark on it .. textfield which i have given is to retrieve from the first name .. When i deleted from the ID i gave as Long.parse(Id_field.getext().tostring) to delete the row from first name .. what should i add ??? Hope u dnt mind with ma silly questions .. i'm ew to it .. and trying to understand .. – Sai Krishna Sep 18 '11 at 16:14