0
每當我在數據庫中LongPress一個項目將其刪除時,它就會崩潰。這裏是我的代碼Android:不能刪除數據庫中的項目
public class MyListViewLongClickListener implements OnItemLongClickListener {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) {
String itemInTheRow = adapter.getItem(position).toString();
String[] details = itemInTheRow.split(" - ");
final String name = details[0];
String brand = details[1];
Integer qty = Integer.parseInt(details[3]);
Double size = Double.parseDouble(details[2]);
Double price = Double.parseDouble(details[4]);
AlertDialog.Builder confirmDialog = new AlertDialog.Builder(DeleteActivity.this);
confirmDialog.setTitle("Confirm Deletion");
confirmDialog.setMessage("Are you sure you want to \nDelete the record of " + name + "?");
confirmDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dbTools.deleteShoes(name);
loadData();
}
});
confirmDialog.setNegativeButton("Cancel", null);
confirmDialog.show();
return false;
}
}
,這是我在刪除
private String tblname = "dtbShoes1";
private String fld_Name = "Name";
public boolean deleteShoes(String name) {
String deleteQuery = "DELETE FROM " + tblname +
" WHERE " + fld_Name + " = " + name;
openConnection();
db.execSQL(deleteQuery);
closeConnection();
return true;
}
方法,這是我的logcat
10-12 15:50:55.052 1572-1572/com.example.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such column: bbb (code 1): , while compiling: DELETE FROM dtbShoes1 WHERE Name = bbb
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at com.example.finalproject.DatabaseShoes.deleteShoes(DatabaseShoes.java:101)
at com.example.finalproject.DeleteActivity$MyListViewLongClickListener$1.onClick(DeleteActivity.java:96)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
每當我改變
String deleteQuery = "DELETE FROM " + tblname +
" WHERE " + fld_Name + " = " + name;
到
String deleteQuery = "DELETE FROM " + tblname +
" WHERE " + fld_Name + " = " + "name";
它刪除了我的數據庫中的所有記錄。任何幫助將不勝感激。
@JoachimIsaksson是的,你是對的,張貼。 – 2014-10-12 08:11:52