tags:

views:

73

answers:

4

Hi All,

I m trying to delete a record from a SQLiteDB via Android Application,This is the way that i used the code to delete the record

//Snippet of code in my DBAdapter Class

public boolean DeleteRecord(String ContactName) {

    Log.i(TAG, "DeleteRecord(String ContactName)");
    Log.i(TAG, ContactName);
    return db.delete(TABLE_SIMPLETABLE_CLIENT1,KEY_EMPLOYEE_NAME + "="
             +ContactName,null);
    }

and from the another class, i trying to call this method by opening the DB and calling this method and again closing the DB, this is how i have coded,

//Snippet of code in my DBApplication Class

public void onClick(DialogInterface dialog,
    int which) {
DBAdapter.open();
DBAdapter.DeleteRecord(DeleteRecord);  
//DeletRecord is string value that in DB(Sivaram)                       
DBAdapter.close();
}

When i deploy it , i an getting the following error,

08-25 14:52:20.602: ERROR/AndroidRuntime(231): android.database.sqlite.SQLiteException: no such column: Sivaram: , while compiling: DELETE FROM SimpleTable1 WHERE Employee_Name=Sivaram

please help me, in finding out the solution

Thanks in advance...

+2  A: 

I think you have passed a wrong query.like

DELETE FROM SimpleTable1 WHERE Employee_Name=Sivaram;

it must be like this.

DELETE FROM SimpleTable1 WHERE Employee_Name='Sivaram';

check it. '' is must for the Text datatype

Praveen Chandrasekaran
@Nandagopal T: Using positional parameters (`DELETE FROM SimpleTable1 WHERE Employee_Name=?`) is a great way to avoid making this mistake.
CommonsWare
Hi Guys,Thanks a lot for your great response,but can u please show me how to use your ideas in my example, because i tried using db.delete(TABLE_SIMPLETABLE_CLIENT1, KEY_EMPLOYEE_NAME + "=" + 'ContactName', null); ,This way i tried to use,it shows me an error as invalid character constant....Pls help me...
Nandagopal T
The single quotes should be within the double quotes: db.delete(TABLE_SIMPLETABLE_CLIENT1, KEY_EMPLOYEE_NAME + "= '" + ContactName + "'", null)
AshtonBRSC
+1  A: 
Edit-

Try this and chk if it works first

return db.delete(TABLE_SIMPLETABLE_CLIENT1,KEY_EMPLOYEE_NAME + "='"+ContactName+"'",null);

here your string should be in your where clause(3rd position) and you just require to mention your table name as your second parameter i believe

delete(String tableName, String whereClause, String[] whereArgs) {}
return db.delete(TABLE_SIMPLETABLE_CLIENT1,KEY_EMPLOYEE_NAME,new String[]{ContactName});
Rahul
Thanks Rahul, your effort helped me a lot, now it works fine,,Thanks for all your Support....
Nandagopal T
A: 

hi... u can write the below query to delete record by paasing the data as string and based on that u can delete the record from table in database

// delete a title by string name specified
public Cursor deleteTitle(String i) 
{
    return db.query(true, DATABASE_TABLE, new String[] { Key_RowID },
      Key_Name + "= ?", new String[] { Key_Name.toString() }, null, null,
      null, null);
}

The problem with your qyery is that u havent converted your name field in database to string i.e. Key_Name.toString()

Try this out this might help

Rakesh Gondaliya
A: 
public boolean deletename(String rowid) {
  return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowid, null) > 0;
}

Try this code its working fine for me. It deletes records using unique row ID. where row is my primary key.

now if you want to delete using any other than rowid.

try this

public boolean deletename(String eid) {
    getrowid(eid);
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + zCursor.getString(0).toString(), null) > 0;
}
public Cursor getrowid(String e_id) throws SQLException {
  //System.out
   // .println("---------------================Employee ID:" + e_id);
  zCursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ROWID },
    KEY_EID + "= ?", new String[] { e_id.toString() }, null, null,
    null, null);
  if (zCursor != null) {
   zCursor.moveToFirst();
   //System.out.println("---------------================"
    // + zCursor.getString(0));
  }
  return zCursor;
 }

here i am deleting my employee records using the employeeid. so i am finding unique rowid which is my primary key for given record and then deleting the record.

ud_an
Thanks Guys,its working great for me too, because of your response..Thanx a lot...
Nandagopal T