ECNU-DEIT-2015 / DICTIONARY

LJX HQH SJY
Apache License 2.0
0 stars 0 forks source link

从数据库获取的值如何才能通过循环全部输出? #2

Open HanQingHuI opened 6 years ago

HanQingHuI commented 6 years ago
String response;
    //var results = await pool.query("select Ielts.word, Ielts.means from Ielts where id='  "+ j +"  ' ");
    var results = await pool.query("select Ielts.word, Ielts.means from Ielts where id<11");
    List resultlist= results.toList() as List;
      for (int i = 0; i <resultlist.length; i++) {
      response = resultlist[i];
      print(response);
      return response;
  }

貌似处理成list之后仍然无法输出。

HanQingHuI commented 6 years ago

用的是foreach。他本身是一个循环语句。只不过在循环内部需要把获取的值分别赋给数组,再将数组的值赋给string变量。代码如下:

Future<String> getDataFromDB() async {
  var pool = new ConnectionPool(
      host: 'www.muedu.org',
      port: 3306,
      user: 'deit-2015',
      password: 'deit@2015!',
      db: 'project_2015_1',
      max: 5);
  var results = await pool.query("select  pos.name, means.means from words,means,pos where word='" + data + "' and words.ID=means.wordID and means.posID=pos.ID");
  String response;
  var meanLIST = new Map();
  meanLIST[0]=null;
  meanLIST[1]=null;
  meanLIST[2]=null;
  meanLIST[3]=null;
  int i=0;
  await results.forEach((row) { 

     response =''' ${row[0]}  ${row[1]}''';
     meanLIST[i]=response;
     i++;
    //  response=JSON.encode("\u8d44\u6599\uff0c\u6750\u6599");
  }); 
  response=JSON.encode([meanLIST[0],meanLIST[1],meanLIST[2],meanLIST[3]]);
  return response;
}