SQL查询从不同客户的同一个表中汇总不同的值[对于android]

时间:2012-09-27 15:37:35

标签: android sqlite

有人可以帮助我: 我有5个表的DB:DB diagram stat_list表预先填充了5个值(Att,pass,TD,inter,pass)。我想在android中创建一个表,它将显示每个玩家的表Stat_list中所有值的总和。非常感谢!

Table example

1 个答案:

答案 0 :(得分:0)

    String sQry = "SELECT * FROM Stat_list;";
    SQLiteDatabase db = null;
    int count = -1;
    Cursor cursor = null;
    try {
        db = SQLiteOpenHelper.getReadableDatabase();
        cursor = db.rawQuery(sQry, null);
        if (cursor.moveToFirst()) {
            do {
                int playerScore = Integer.parseInt(cursor.getString(1)) +
                                  Integer.parseInt(cursor.getString(2)) +
                                  Integer.parseInt(cursor.getString(3)) +
                                  Integer.parseInt(cursor.getString(4))
                 ContentValues playerScores = new ContentValues();

                 playerScores.put(THE_NAME_OF_THE_SCORE_COLUMN, playerScore);
                 try{
                     db.update(NAME_OF_TABLE_WITH_SUMMED_PLAYER_SCORES,playerScores, "PlayerName = " cursor.getString(1), null);
                    }catch(SQLiteException e){}
                 } 
                 while (cursor.moveToNext());
        }           
    }
    catch (SQLiteException e) {
        Log.d("SQL Error", e.getMessage());
    }
    finally { 
        cursor.close();
        db.close(); 
    }

上面的代码片段从数据库中提取信息,汇总分数并将其插入另一个表中。您必须将int设置为特定值,例如正确的列和表名。