2014-10-12 49 views
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"; 

它刪除了我的數據庫中的所有記錄。任何幫助將不勝感激。

回答

3

你錯過了周圍的列名的單引號,所以改變

String deleteQuery = "DELETE FROM " + tblname + 
       " WHERE " + fld_Name + " = " + name; 

String deleteQuery = "DELETE FROM " + tblname + 
       " WHERE " + fld_Name + " ='" + name + "'"; 

,並特別建議的方法是使用參數化查詢作爲

db.delete(tblname, fld_Name + " = ?", new String[] { name }); 
+1

@JoachimIsaksson是的,你是對的,張貼。 – 2014-10-12 08:11:52

0

這是刪除方法我用於我的應用程序刪除項目在一次:

public void delete_cartitem(int id) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_NAME, KEY_ID + "=?", 
      new String[] { String.valueOf(id) }); 
    db.close(); 
} 

您可以根據您的要求更改。

相關問題