我正在尝试构建一个非常简单的 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 工作正常,问题是 lossScheduleQuery 和 winScheduleQuery。日志还报告 winScheduleQuery 和 lossScheduleQuery 正如我认为的那样。