samedi 12 mars 2016

Multiple SQL queries in Android SQLite

Basically, I am trying to find the sums of all the ages in my table and then to update the value in another table. I know how to do this with two SQL Statements but I don't know how to combine this in android.

This is what I have and struggling on - I am basically trying to get the total or SUM of all the ages in my search query and insert that into a second table. Below are my two SQL statements, and the below code does not work as I don't know how to proceed:

public int totalAges(String userID){
    SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
    String stmt1 = "SELECT SUM(users.age) FROM users
                            INNER JOIN overalls
                            ON users.userID=overalls.userID AND users.userID=?";
    // I want to insert this result into the field totalage in the overalls table.
    String stmt2 = "UPDATE overalls SET totalage = (the result from stmt1) WHERE overalls.userID";
    Cursor cursor = sqLiteDatabase.rawQuery(stmt1, new String[]{Integer.toString(diaryID)});
    int age=0;
    return age;
}

Would be nice if someone could help me out - I am so close

Thanks

Aucun commentaire:

Enregistrer un commentaire