Reputation: 5083
I'm unable to delete data from ListView
. I'm using SQLite
for the first time and I'm not able to detect the error here. I checked other posts but they were talking about the formatting of the query. I don't think that's the problem in this case, or is it?
LogCat:
07-31 14:33:20.268: E/SQLiteLog(16651): (1) no such column: aasdfghn
07-31 14:33:20.283: E/AndroidRuntime(16651): FATAL EXCEPTION: main
07-31 14:33:20.283: E/AndroidRuntime(16651): android.database.sqlite.SQLiteException: no such column: aasdfghn (code 1): , while compiling: DELETE FROM Accounts WHERE name = aasdfghn
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1547)
07-31 14:33:20.283: E/AndroidRuntime(16651): at com.example.dialog.DataBox.deleteItem(DataBox.java:60)
07-31 14:33:20.283: E/AndroidRuntime(16651): at com.example.dialog.MainActivity$MyAdapter$1.onClick(MainActivity.java:170)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.view.View.performClick(View.java:4231)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.view.View$PerformClick.run(View.java:17537)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.os.Handler.handleCallback(Handler.java:725)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.os.Looper.loop(Looper.java:158)
07-31 14:33:20.283: E/AndroidRuntime(16651): at android.app.ActivityThread.main(ActivityThread.java:5751)
07-31 14:33:20.283: E/AndroidRuntime(16651): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 14:33:20.283: E/AndroidRuntime(16651): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 14:33:20.283: E/AndroidRuntime(16651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
07-31 14:33:20.283: E/AndroidRuntime(16651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
07-31 14:33:20.283: E/AndroidRuntime(16651): at dalvik.system.NativeStart.main(Native Method)
Code:
public class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context cc) {
super(cc, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
Toast.makeText(getApplicationContext(), "Removed", 2000).show();
onCreate(db);
}
}
public void deleteItem(String nn) throws java.sql.SQLException{
db.delete(TABLE_NAME , "name = " + nn , null);
}
MainActivity:
holder.n.setText(details.get(position).getName());
// holder.i.setText(details.get(position).getId());
// String idd= holder.i.getText().toString();
final String nn = holder.n.getText().toString();
holder.p.setText(details.get(position).getPassword());
// final String pp = holder.p.getText().toString();
// final int id=Integer.parseInt(idd.toString());
holder.delBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BOX.open();
try {
BOX.deleteItem(nn);
} catch (SQLException e) {
e.printStackTrace();
}
BOX.close();
}
});
Upvotes: 1
Views: 485
Reputation: 11027
The answer is in the first lines of the exception you are receiving:
(1) no such column: aasdfghn
...
no such column: aasdfghn (code 1): , while compiling: DELETE FROM Accounts WHERE name = aasdfghn
The name "aasdfghn" is taken for a column name by SQLite. The bug comes from the BOX.deleteItem()
method:
public void deleteItem(String nn) throws java.sql.SQLException {
db.delete(TABLE_NAME , "name = " + nn , null); // quotes are missing around the name.
// Should be: db.delete(TABLE_NAME, "name='" + nn + "'" ,null);
}
Upvotes: 3
Reputation: 24848
Try this way,hope this will help you to solve your problem.
public void deleteItem(String nn) throws java.sql.SQLException{
String whereClause = "name"+"=?";
String[]whereArgs = new String[] {nn};
db.delete(TABLE_NAME, whereClause , whereArgs);
}
holder.delBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BOX.open();
try {
BOX.deleteItem(nn);
} catch (SQLException e) {
e.printStackTrace();
}
BOX.close();
notifyDataSetChanged();
}
});
Upvotes: 0