我試圖構建一個非常簡單的android應用程序,它在使用MySQL查詢向數據庫接收到MainActivity類的值後,在GridView中顯示一些數據。我已經測試了MySQL服務器上的查詢並且它們是正確的,但是當我在應用程序中運行它們時,它們全都返回恰好一行,實際上每次運行時它們都應該從0返回到任何地方到10行左右,這對於每一行的teamQuery結果。所以有些奇怪的事情正在發生,但我無法弄清楚。下面是它運行的查詢活動類....Android應用程序中的MySQL查詢返回錯誤的值
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類拆分它從servlet接收以換行符(\ n)的,這就是爲什麼我分裂lossResult,winResult和teamResult與行'\ n'將查詢結果分成若干行。日誌報告「NUMBER_OF_COACHES」是正確的,所以teamQuery工作正常,它是lossScheduleQuery和winScheduleQuery,這是問題所在。該日誌還報告winScheduleQuery和lossScheduleQuery正是我認爲他們應該是。