使用grails导入文件CSV时获取下一行

时间:2014-03-21 09:16:11

标签: mysql sql grails csv groovy

我正在尝试导入文件CSV,如此图片..

此图片表示..

当我导入此文件时,将行1读取并保存到Table SeniorHighSchool 那么它会得到:

  1. 姓名:Alexander
  2. 年龄:15
  3. muchcourse:3
  4. 之后我想要做一个条件,当"多道次被填满"它会读下一行...... 例如: 在这种情况下," muchcourse"是" 3",然后"看图像" ..行2,3,4(3行)将被插入到其他表格中...因为" muchcourse&#34 ;是" 3"

    enter image description here

    这是我编写的,我尝试过的。

    def upload = {
                withForm{
                    def f = request.getFile('filecsv')
                    def orifilename = f.getOriginalFilename()
                    def homeDir = new File(System.getProperty("user.home")) 
                    def homeurl = "Documents/Uploads/"
                    File fileDest = new File(homeDir,homeurl+orifilename)
                    f.transferTo(fileDest)  
    
                    request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
                    def student= new SeniorHighSchool(
                        name: fields[0].trim(),
                        age: fields[1].trim(),
                        muchcourse: fields[2].trim()
    
                        )
                    if (student.hasErrors() || student.save(flush: true) == null)
                    {
                        log.error("Could not import domainObject  ${student.errors}")
                    }
                    }
    
                    redirect(action:"list")
    
                }
                    }
    

    我很困惑,以制造一个条件..

      def upload = {
                        withForm{
                            def f = request.getFile('filecsv')
                            def orifilename = f.getOriginalFilename()
                            def homeDir = new File(System.getProperty("user.home")) 
                            def homeurl = "Documents/Uploads/"
                            File fileDest = new File(homeDir,homeurl+orifilename)
                            f.transferTo(fileDest)  
    
                            request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
                            def student= new SeniorHighSchool(
                                name: fields[0].trim(),
                                age: fields[1].trim(),
                                muchcourse: fields[2].trim()
    
                                )
                            if (student.hasErrors() || student.save(flush: true) == null)
                            {
                                log.error("Could not import domainObject  ${student.errors}")
                            }
    
                           if(fields[2]) {
                           def score = new Score(
                           course: //the problem at this line..how?
                                   //it will insert 3 times then back to the row 5 to insert into "Student" again
                            )
    
    
    
        }
                            }
    
                            redirect(action:"list")
    
                        }
    
    
     }
    

3 个答案:

答案 0 :(得分:2)

if(fields.size()>2){
  store 3 values in one table(student)
}
else{
  store 2 values in another table(score)
}

如果出现多道场,则字段大小为3,然后在一个表中保存三个数据。否则大小为2然后将另外两个数据保存在另一个表中。我认为它将解决您的问题。

答案 1 :(得分:0)

@ th3morg喜欢这个?

request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
if(fields.size()>2){
  def student= new SeniorHighSchool(
                    name: fields[0].trim(),
                    age: fields[1].trim(),
                    muchcourse: fields[2].trim()
                    )
}
else{
  def score = new Score(
  course:fields[0].trim(),
  score:fields[1].trim()  
  )
}



}

答案 2 :(得分:0)

如果上课SeniorHighStudent“static hasMany = [scores:Score]”那么以下应该可以做到这一点:

    def currentStudent
    request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
        if(fields.size()>2){
            if(currentStudent){
                /*we've found a new student, so save the previous one*/
                currentStudent.save()
            }
            currentStudent = new SeniorHighSchool(
                    name: fields[0].trim(),
                    age: fields[1].trim(),
                    muchcourse: fields[2].trim()
            )
        }
        else{
            /*add the score to the currentStudent's scores*/
            currentStudent.addToScores(new Score(
                    course:fields[0].trim(),
                    score:fields[1].trim()
            ))
        }
    }
    /*when the loop is done, save the last student because it hasn't been saved yet*/
    currentStudent.save()
相关问题