如果项目已存在,如何添加数量

时间:2018-03-23 06:40:37

标签: java android

我想检查记录是否存在。如果存在,我想添加数量。

btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = etCode.getText().toString();
                String product = etProduct.getText().toString();
                String qty = etQty.getText().toString();
                try {
                    if (isExist(code)) {
                        rQty = Integer.parseInt(rQty + etQty.getText().toString());
                    } else {
                        myDB.addData(code, product, Integer.parseInt(qty));
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();

                    }
                } catch (Exception ex) {
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
 public boolean isExist(String code) {
        boolean result;
        SQLiteDatabase db = myDB.getReadableDatabase();
            try {
            Cursor cursor = db.rawQuery("SELECT QTY FROM users_data WHERE CODE " + code + "'", null);
            if (cursor.getCount() > 0 ){
                while (cursor.moveToFirst()){
                    rQty = cursor.getInt(cursor.getColumnIndex("QTY"));
                }
                result = true;
            }else {
                result = false;
            }
        }
        catch (Exception ex){
            result = false;
        }
        return result;
    }
}

这是code.if插入相同的项目代码,数据变成重复,不添加相同代码的数量。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误和错误。一个问题是这个代码

 rQty = Integer.parseInt(rQty + etQty.getText().toString());

它不会在输入数量上添加qQty。因为这里的字符串连接起来。 你必须使用

 rQty += Integer.parseInt (qty);

第二个是你的sqlite查询不对,它是不完整的 所以改变

 Cursor cursor = db.rawQuery("SELECT QTY FROM users_data WHERE CODE " + code + "'", null);

 Cursor cursor = db.rawQuery("SELECT QTY FROM users_data WHERE CODE = '" + code + "'", null);

尝试以下代码

  btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code = etCode.getText().toString();
            String product = etProduct.getText().toString();
            String qty = etQty.getText().toString();
            try {
                if (isExist(code)) {
                    rQty += Integer.parseInt (qty);
                } else {
                    myDB.addData(code, product, Integer.parseInt(qty));
                    Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();

                }
            } catch (Exception ex) {
                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
            }
        }
    });
}
public boolean isExist(String code) {
    boolean result;
    SQLiteDatabase db = myDB.getReadableDatabase();
    try {
        Cursor cursor = db.rawQuery("SELECT QTY FROM users_data WHERE CODE = '" + code + "'", null);
        if (cursor.getCount() > 0 ){
            while (cursor.moveToFirst()){
                rQty = cursor.getInt(cursor.getColumnIndex("QTY"));
            }
            result = true;
        }else {
            result = false;
        }
    }
    catch (Exception ex){
        result = false;
    }
    return result;
}