每当我在数据库中长按一个项目将其删除时,它就会崩溃。这是我的代码
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";
它删除了我在数据库中的所有记录。任何帮助将不胜感激。
答案 0 :(得分: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 :(得分:0)
这是我在我的应用程序中用于一次删除项目的删除方法:
public void delete_cartitem(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + "=?",
new String[] { String.valueOf(id) });
db.close();
}
您可以根据自己的要求进行更改。