从 SQLite 数据库中获取数据时有任何响应

There is Any response during getting data from SQLite database

首先,我的情况没有错误,但是当我触摸上下文菜单时有任何反应'delete'。电脑好像一直处于不可读状态

getAmount 调用量 DBHelper.java

public int getAmount(Integer id){
    int amount = 0;
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT id, amount FROM uselist WHERE id="+id,null);
    amount=res.getInt(res.getColumnIndex(USELIST_COLUMN_AMOUNT));
    return amount;
}

getAmount上面的代码用到了

@Override
public boolean onContextItemSelected(MenuItem item){
    int itemId = item.getItemId();
    switch (itemId){
        case R.id.context_item1 :
            Toast.makeText(this,"edit",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.context_item2 :
            int amt=-mydb.getAmount(position);
            Toast.makeText(this,"delete",Toast.LENGTH_SHORT).show();
            mydb.deleteUseList(position);
            mydb.update("UPDATE uselist SET id = (id-1) WHERE id >"+ position+";");
            mydb.update("UPATE uselist SET balance = (balance+"+amt+") WHERE id>"+position+";");
            histories.clear(); //
            finish();
            startActivity(getIntent());
            return true;
    }
    return super.onContextItemSelected(item);
}

我猜 'int amt=-mydb.getAmount(position);'code 或 getAmount class 有问题,因为 delete Toast 没有出现。

试试这个:

public int getAmount(Integer id){
    int amount = 0;
    SQLiteDatabase db=this.getReadableDatabase();

    Cursor res = db.rawQuery("SELECT id, amount FROM uselist WHERE id="+id,null);

            // looping through all rows and setting value to variable
            if (res.moveToFirst()) {
                do {
                    amount=res.getInt(res.getColumnIndex(USELIST_COLUMN_AMOUNT));
                } while (res.moveToNext());
            }

     return amount;
   }