1

我有简单的列表视图,它显示名称和编号(此数据实际上来自数据库)。当我尝试删除其工作正常(对于数字字段为“123456”)但问题无法删除一些数字字段为“888-888-00”的列表项

这里添加了我的代码......请帮帮我......,

数据库助手.java

private static final String KEY_NAME = "name";
private static final String KEY_NUMBER = "number";

public void deleteContacts(String name, String number){
    //Raw Query wont work for Deletion
    db = this.getWritableDatabase();
    System.out.println("deleted number: "+number+"name: "+name);
    String deleteQuery = "Delete from "+CONTACTS_TABLE_NAME+" where "+KEY_NUMBER+" = "+number;
    //db.rawQuery("Delete from "+CONTACTS_TABLE_NAME+" where "+KEY_NUMBER+" = "+number, null);
    db.execSQL(deleteQuery);
    db.close();
}

MainActivity.java

在 oncreate() 方法内部包含,

selectedListView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            final HashMap<String, String> toDelete = (HashMap<String, String>) selectedListView.getItemAtPosition(position);

            AlertDialog.Builder deleteDialog = new AlertDialog.Builder(MainActivity.this);
            deleteDialog.setTitle("Confirm Delete...");
            deleteDialog.setMessage("Are you sure you want delete this '"+toDelete.get("name")+"' Contact?");

            deleteDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    dataBaseHelpher.deleteContacts(toDelete.get("name"), toDelete.get("number"));
                    dialog.dismiss();
                    updateList();
                }
            });
            deleteDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });
            deleteDialog.show();
        }
    });
4

2 回答 2

2

您需要用引号将您的号码括起来,因为 '888-888-888' 是字符串而不是整数。

String deleteQuery = "delete from " + CONTACTS_TABLE_NAME + " where " + KEY_NUMBER + " = '" + number + "'";

最好使用预先准备好的语句,像这样。

SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("delete from ? where ? = ?");
stmt.bindString(1, CONTACTS_TABLE_NAME);
stmt.bindString(2, KEY_NUMBER );
stmt.bindString(3, number);
stmt.execute();
于 2013-09-14T22:40:41.247 回答
1

试试这个代码 -

db.delete("Your Table Name", "KEY_NUMBER = " + number, null);
于 2013-09-14T22:11:08.177 回答