简体   繁体   English

Android SQLite - 根据表 2 中的更改更新表 1

[英]Android SQLite - Update table 1 based on changes in table 2

I am working on a android app and i have two sqlite tables.我正在开发一个 android 应用程序,我有两个 sqlite 表。

projects
id     projectname    projectstatus
1      TestProj1             1
2      TestProj2             1

outlets
id    pid    outletname    status
1      1      testoutlet1    1
2      1      testoutlet2    1
3      2      testoutlet1    1

Under each project I may have a single outlet or multiple outlets .在每个项目下,我可能有一个出口或多个出口 The user is expected to perform certain actions in each outlet.用户应该在每个插座中执行某些操作。 Only the projects and outlets with status 1 is visible to the user.只有状态为 1 的项目和出口对用户可见。

The pid in the outlet table refers to id of projects table. outlet 表中的pid 是指projects 表的id。 At the outlet level when the user completes the task I am updating the status to 2. Upon updating the status to 2 for each outlet within the project, I am trying to update the status of the project to 2.在用户完成任务时的出口级别,我将状态更新为 2。在将项目中的每个出口的状态更新为 2 后,我试图将项目的状态更新为 2。

For example, the first 2 outlets are within TestProj1 as we can see the pid is 1. The following code sets the status of outlets to 2.例如,前 2 个出口在 TestProj1 中,因为我们可以看到 pid 为 1。以下代码将出口的状态设置为 2。

private void updateOutletStatus() {
        String query = "select OutletName from projectparams where projectName= '"+projectName+"' and OutletName= '"+outletName+"' group by projStatus";
        if (sqLiteHelper.getData(query).getCount()==1) {
            Toast.makeText( getApplicationContext(), "No more tasks in this outlet!", Toast.LENGTH_SHORT ).show();
            SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
            String outletUpdate = "update outlets set status=2 where OutletName='"+outletName+"' and pid = (select id from projects where projectName = '"+projectName+"' ) ";
            db.execSQL(outletUpdate);
        }
    }

The above code works correctly.上面的代码工作正常。 When the outlet status for all the outlets within a project becomes 2 I want to set the projectstatus to 2. I tried the following code:当项目中所有插座的插座状态变为 2 时,我想将 projectstatus 设置为 2。我尝试了以下代码:

private void updateProjectStatus() {
        String query = "select Status from outlets where pid = (select id from projects where ProjectName = '"+projectName+"' )  group by Status";
        if (sqLiteHelper.getData(query).getCount()==1) {
            Toast.makeText( getApplicationContext(), "No more outlets in this project!", Toast.LENGTH_SHORT ).show();
            SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
            String projectUpdate = "update projects set projectStatus=2 where pid = (select id from projects where ProjectName = '"+projectName+"' ) ";
            db.execSQL(projectUpdate);
        }
    }

The above is not working for project update.以上不适用于项目更新。 I tried various combinations but still doesn't work我尝试了各种组合但仍然不起作用

Perhaps consider using the following :-也许考虑使用以下内容:-

private void updateProjectStatus() {
        db.execSQL("UPDATE projects SET projectstatus = 2 WHERE id IN (SELECT projects.id FROM projects JOIN outlets ON projects.id = outlets.pid WHERE projectstatus < 2 AND projectname ='" + projectName + "' GROUP BY pid HAVING (count() * 2) = sum(status))");
}
  • Note, if you removed AND projectname ='" + projectName + "' then it would update all projects that hadn't already had the status set to 2 and that had all outlets set to a status of 2 (in which case there would be no values to be bound).请注意,如果您删除了AND projectname ='" + projectName + "'那么它会更新所有尚未将状态设置为 2 并且所有出口都设置为状态 2 的项目(在这种情况下,将有没有要绑定的值)。

It is generally considered better to not insert values via concatenation but to instead bind the values passed via the 2nd argument of execSQL .通常认为最好不要通过串联插入值,而是绑定通过execSQL的第二个参数传递的值。

  • binding values via arguments has a number of advantages通过参数绑定值有很多优点
    • the SQL is shorter SQL 更短
    • there is less chance for errors as the values are correctly enclosed由于正确包含值,因此出错的可能性较小
    • it provides protection against SQLInjection它提供了针对 SQLInjection 的保护

The above would then become (the ? representing the value to be bound)然后上面的将变成(代表要绑定的值的?)

private void updateProjectStatus() {
        db.execSQL("UPDATE projects SET projectstatus = 2 WHERE id IN (SELECT projects.id FROM projects JOIN outlets ON projects.id = outlets.pid WHERE projectstatus < 2 AND projectname = ? GROUP BY pid HAVING (count() * 2) = sum(status))", new String[]{projectName});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM