简体   繁体   中英

Android SQLite - Update table 1 based on changes in table 2

I am working on a android app and i have two sqlite tables.

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.

The pid in the outlet table refers to id of projects table. 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.

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.

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:

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).

It is generally considered better to not insert values via concatenation but to instead bind the values passed via the 2nd argument of execSQL .

  • binding values via arguments has a number of advantages
    • the SQL is shorter
    • there is less chance for errors as the values are correctly enclosed
    • it provides protection against 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});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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