Android应用程序中MySQL查询的返回值错误

时间:2013-02-03 08:13:18

标签: android mysql servlets android-arrayadapter

我正在尝试构建一个非常简单的Android应用程序,它在使用对数据库的MySQL查询从MainActivity类接收值后,在GridView中显示一些数据。我在MySQL服务器上测试过它们是正确的,但是当我在应用程序中运行它们时,它们都返回正好一行,而实际上每次运行时都会运行winScheduleQuery和lossScheduleQuery返回0到10行左右的任何位置,这对于teamQuery结果的每一行都是如此。所以有些奇怪的事情正在发生,但我无法弄清楚。这是运行查询的活动类....

public class ViewTeamsActivity extends Activity {

      ArrayList<String> scheduleData = new ArrayList<String>();
      String dcode;

      private void initClasses(String dcode){
        String teamResult = "";
        MySQLClient mc = 
            new MySQLClient("http://someServletURL/servlet/EMBLServices");
        try {
          String teamQuery = 
            "SELECT coach, tno FROM team WHERE dcode='" + dcode + "'";

          teamResult = mc.runSQL(teamQuery);
          String[] rows = teamResult.split("\n");
          Log.d("NUMBER_OF_COACHES", rows.length + "");
          String winResult = "";
          String lossResult = "";
          for(int i = 1; i < rows.length; i++){
            String[] cols = rows[i].split(",");
            String winScheduleQuery = 
                      "(SELECT * FROM schedule WHERE dcode='" + dcode + "' AND hometeam=" + cols[1] + 
                            " AND (homescore > awayscore))" +
                            " UNION ALL " +
                            "(SELECT * FROM  schedule WHERE dcode='" + dcode + "' AND awayteam=" + cols[1] + 
                            " AND (awayscore > homescore))";

            String lossScheduleQuery = 
                      "(SELECT * FROM schedule WHERE dcode='" + dcode + "' AND hometeam=" + cols[1] + 
                      " AND (homescore < awayscore))" +
                      "UNION ALL" +
                      "(SELECT * FROM schedule WHERE dcode='" + dcode + "' AND awayteam=" + cols[1] + 
                      " AND (awayscore < homescore))";

            winResult = mc.runSQL(winScheduleQuery);
            lossResult = mc.runSQL(lossScheduleQuery);
            String[] winRows = winResult.split("\n");
            String[] lossRows = lossResult.split("\n");
            scheduleData.add(cols[0] + "");
            scheduleData.add(winRows.length + "");
            scheduleData.add(lossRows.length + "");

            Log.d("Win query", winScheduleQuery);
            Log.d("Loss query", lossScheduleQuery);

          }
        } catch (Exception e) {
            Log.d("MyAppException",e.getMessage());
          } 

      }


      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_view_teams);

        if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }

        dcode = getIntent().getStringExtra("DCODE");
        initClasses(dcode);

        GridView scheduleDataGridView = (GridView)findViewById(R.id.gridview);
        ArrayAdapter<String> teamArrayAdapter = 
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, scheduleData);    
        scheduleDataGridView.setAdapter(teamArrayAdapter);
      }
}

SQLClient类使用换行符(\ n)将从servlet接收的行拆分,这就是为什么我将lossResult,winResult和teamResult与'\ n'分开以将查询结果划分为行。日志报告“NUMBER_OF_COACHES”是正确的,因此teamQuery正常工作,它是lostScheduleQuery和winScheduleQuery这就是问题所在。 Log还报告了winScheduleQuery和lossScheduleQuery,正如我认为的那样。

1 个答案:

答案 0 :(得分:0)

我明白了。我使用的是其他人编写的MySQLClient类,并且由于某种原因,它不接受select语句的括号....