当前位置: 代码迷 >> Android >> sqlite多次删除记录的有关问题
  详细解决方案

sqlite多次删除记录的有关问题

热度:55   发布时间:2016-05-01 21:16:35.0
sqlite多次删除记录的问题?
删除代码如下:
//删除操作
public void delete(int id) {
if (db == null) {
db = this.getWritableDatabase();
}

db.delete(TBL_NAME, "_id=?", new String[] {String.valueOf(id)});
}

连续两次删除就会报错,调用删除语句为:
myList.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView idView = (TextView) arg0.findViewById(R.id.mytext0);
final int id = Integer.parseInt(idView.getText().toString());

Log.i("OOOOOOOOOOOOOOOOOOO", id+"");

builder.setMessage("确定删除该记录吗?").setCancelable(false).setPositiveButton("确定", 
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

SQLiteDatabase ddb = SQLiteDatabase.openOrCreateDatabase("/data/data/com.chenlong12580.login/databases/mymenu.db", null);

//删除数据
//helper.delete(id);

//重新查询
Cursor c = helper.query();

startManagingCursor(c);

if (c.moveToFirst()) {

//列表项数组
String from[] = {"_id", "caiId", "name", "price", "number", "kouwei"};
 
//列表ID
int[] to = {R.id.mytext0, R.id.mytext1, R.id.mytext2,
R.id.mytext3, R.id.mytext4, R.id.mytext5};
 
//适配器
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MyMenuActivity.this, R.layout.mymenulist_item,
c, from, to);
 
//设置适配器
myList.setAdapter(adapter);
 

if (ddb != null) {
ddb.close();
}

helper.close();
}
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}});

AlertDialog dialog = builder.create();

dialog.show();

return true;

}

});


连续两次删除就会报错,希望大家给与解答,报错如下:
E/AndroidRuntime(471): java.lang.NullPointerException
E/AndroidRuntime(471): at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:283)
E/AndroidRuntime(471): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
E/AndroidRuntime(471): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1760)
E/AndroidRuntime(471): at com.chenlong12580.login.MyDBHelper.delete(MyDBHelper.java:112)
 E/AndroidRuntime(471): at com.chenlong12580.login.MyMenuActivity$1$1.onClick(MyMenuActivity.java:58)

------解决方案--------------------
第一次操作后helper.close已经关闭了数据库,去掉helper.close试试
  相关解决方案