Android: SQLite Not able to delete a specific record
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...
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});
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
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.
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
精彩评论