0

GROUP BY当我在android中使用时,这段代码有什么问题 。请在 sqlite 中测试此代码:

public void getPlayerName_OrderBy(int First_TeamID)
 {
       int Match_ID = get_MatchID();         

       int matchId;
       int teamID;
       int PlayerID;
       String Name;
        if(Player_SD != null)
            Player_SD.clear();

               String selectQuery = "SELECT "+ STStrikerPlayerName+", count +"("+STStrikerPlayerName+")" +" FROM " +TABLE_SCORE+" where "+ STMatchID +" = '"+ Match_ID +"'"+" AND "      +STFristTeamID + "= '"+First_TeamID+"'"+" GROUP BY "+STStrikerPlayerName;

       SQLiteDatabase db = this.getWritableDatabase();
       Cursor cursor = db.rawQuery(selectQuery, null);

       if (cursor.moveToFirst()) {
            do {
                Player_score_details psd = new Player_score_details();

                matchId = Integer.parseInt(cursor.getString(0));    
                teamID = Integer.parseInt(cursor.getString(1));                                     


                PlayerID = Integer.parseInt(cursor.getString(3));                   


                Name = cursor.getString(20); 



            } while (cursor.moveToNext());
        }

        db.close();    


 }
4

2 回答 2

3

你可以使用以下

/**
     * Selects records from table
     * 
     * @param table
     *            = name of table
     * @param columns
     *            = String[] for columns
     * @param where
     *            = WHERE condition
     * @param whereargs
     *            = arguments if where parameter is in prepared statement format
     * @param groupby
     *            = GROUP BY column(s)
     * @param having
     *            = HAVING condition
     * @param orderby
     *            = ORDER BY column witrh asc, desc specification
     * @return
     */
    public List<Object[]> select(String table, String columns[], String where,
            String whereargs[], String groupby, String having, String orderby)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(table, columns, where, whereargs,
                groupby, having, orderby);      
        List<Object[]> list = parseCursorToList(cursor);
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return list;
    }
于 2013-09-11T13:50:31.323 回答
0

这是主要解决方案...

公共无效 getPlayerName_OrderBy(int First_TeamID) { int Match_ID = get_MatchID();

       int matchId;
       int teamID;
       int PlayerID;
       String StrikerName;
        if(Player_SD != null)
            Player_SD.clear();

       String selectQuery = "SELECT "+STMatchID+","+ STFristTeamID+","+ STStrikerPlayerID +","+ STStrikerPlayerName+", count "+"("+STStrikerPlayerName+")"+" FROM " 
                                     +TABLE_SCORE+" where "+ STMatchID +" = '"+ Match_ID +"'"+" AND "
                                     +STFristTeamID + "= '"+First_TeamID+"'"+" GROUP BY "+STStrikerPlayerName;

       SQLiteDatabase db = this.getWritableDatabase();
       Cursor cursor = db.rawQuery(selectQuery, null);

       if (cursor.moveToFirst()) {
            do {
                Player_score_details psd = new Player_score_details();

                matchId = cursor.getInt(cursor.getColumnIndex(STMatchID));
                psd.MTMID = cursor.getInt(cursor.getColumnIndex(STMatchID));

                teamID = cursor.getInt(cursor.getColumnIndex(STFristTeamID));                               
                psd.MTID = cursor.getInt(cursor.getColumnIndex(STFristTeamID));  

                PlayerID = cursor.getInt(cursor.getColumnIndex(STStrikerPlayerID));  
                psd.MTPID = cursor.getInt(cursor.getColumnIndex(STStrikerPlayerID));  

                StrikerName = cursor.getString(cursor.getColumnIndex(STStrikerPlayerName));  
                psd.PlayerName = cursor.getString(cursor.getColumnIndex(STStrikerPlayerName)); 

                Player_SD.add(psd);

            } while (cursor.moveToNext());
        }

        db.close();
}
于 2013-09-12T06:53:36.503 回答