数据库未将数据从一项活动更新到另一项活动

时间:2019-05-30 02:01:52

标签: android sqlite

我需要从DeleteLayout传递数据以更新historyActivity中的数据,该历史数据将在我编辑数据后更新。它不会引发任何错误,但是编辑后我的数据不会更新。如何在活动之间传递数据并更新数据库。

我在DatabaseHelper中的updateData方法如下:

    public boolean updateData(String goalDate, String goalWeight, String currentWeight){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
        contentValues.put(Col2, goalDate);
        contentValues.put(Col3, goalWeight);
        contentValues.put(Col4, currentWeight);
        db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
        return true;
    }

删除布局

   protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deletelayout);
        GoalDate = findViewById(R.id.goalDate2);
        CurrentWeight = findViewById(R.id.weighGoal2);
        GoalWeight = findViewById(R.id.weightCurrent2);
        goalD = findViewById(R.id.goalDateInput);
        goalW = findViewById(R.id.goalWeightInput);
        currentW = findViewById(R.id.currentWeightInput);

        imageView = findViewById(R.id.imageShow);
        data = myDB.getListContents();
        deleteB = findViewById(R.id.deleteB);
        updateB = findViewById(R.id.updateButton);
        // Entered information from the user are set to the respective variables.
        int numRows = data.getCount();
        j = new Intent(DeleteLayout.this, historyActivity.class);

        if (numRows == 0) {
            Toast.makeText(DeleteLayout.this, "Nothing in the db", Toast.LENGTH_LONG).show();
        } else {
            int i = 0;
            while (data.moveToNext()) {
                user = new User(data.getString(1), data.getString(2), data.getString(3), data.getString(4), data.getString(5));
                gWt = user.getGoalWeight();
                cWt = user.getCurrentWeight();
                gDt = user.getGoalDate();
                GoalDate.setText(gDt);
                GoalWeight.setText(gWt);
                CurrentWeight.setText(cWt);

                deleteB.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDB.deleteContent(GoalDate.getText().toString(), GoalWeight.getText().toString(), CurrentWeight.getText().toString());
                        startActivity(j);
                    }
                });
                updateB.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        boolean myUpdate = myDB.updateData(goalD.getText().toString(), goalW.getText().toString(), currentW.getText().toString());
                        if(myUpdate == true){
                            Toast.makeText(DeleteLayout.this, "Data updated", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(DeleteLayout.this, "Data not updated", Toast.LENGTH_SHORT).show();

                        }
                        startActivity(j);
                    }
                });
            }

        }

    }

1 个答案:

答案 0 :(得分:2)

问题

您遇到的一个问题是您不要求进行任何更改。

此外,您始终假设一行或多行一直被更新,即您不检查是否已执行任何更新。

  • SQliteDatabase update 方法返回一个整数,该整数具有已更新的行数。如果没有更新,则将返回0。

说明

考虑 goalDate 2019-01-01 ,而 goalWeight 100 currentWeight < / strong>是 200 ,那么您说的是

  • 更新
    • goalDate 2019-01-01
    • 目标体重 100
    • 当前重量 200
  • 对于其中的行
    • goalDate 2019-01-01
    • 目标体重 100
    • currentWeight 200

修复

代替:-

public boolean updateData(String goalDate, String goalWeight, String currentWeight){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
    contentValues.put(Col2, goalDate);
    contentValues.put(Col3, goalWeight);
    contentValues.put(Col4, currentWeight);
    db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
    return true;
}

您想要的是类似:-

的东西
public boolean updateData(String goalDate, String goalWeight, String currentWeight, String newGoaldate, String newGoalWeight, String newCurrentWeight){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";
    contentValues.put(Col2, newGoalDate);
    contentValues.put(Col3, newGoalWeight);
    contentValues.put(Col4, newCurrentWeight);
    if (db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight}) > 0) {
        return true;
    }
    return false;
}
  • 前三个参数是原始值(用于定位要更新的行),第二个参数是用于更新一个或多个定位行的值。

额外

理想情况下,您还应该更改:-

 String where = "GDATE = ? AND GWEIGHT = ?  AND CWEIGHT = ?";

至:-

String where = Col2 + "=? AND " + Col3 + "=?  AND " + Col4 +"=?";