2 o casper ne O_Casper_ne 于 2013.09.29 13:40 提问

当 string 中包含“-”时,不能删除 sqlite 中的指定行

在下面的 listview 中显示名字和号码,这些数据都是从数据库中获取。当我删除比如数字区域是 "123456"时,能正常运行。但是问题是不能删除一些 list items ,比如它的数字区域是 "888-888-00"。
我使用的代码 :
DatabaseHelpher.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() method 方法中包括:

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();
        }
    });
Csdn user default icon
上传中...
上传图片
插入图片