Mysql 使用grails导入CSV文件时获取下一行
我正在尝试导入类似此图像的CSV文件 这个图像的意思是 当我导入此文件时..第1行读取并保存到SeniorHighSchool表 然后它将得到:Mysql 使用grails导入CSV文件时获取下一行,mysql,sql,grails,csv,groovy,Mysql,Sql,Grails,Csv,Groovy,我正在尝试导入类似此图像的CSV文件 这个图像的意思是 当我导入此文件时..第1行读取并保存到SeniorHighSchool表 然后它将得到: 姓名:亚历山大 年龄:15 多数课程:3 之后,我想做一个条件,当“大部分课程已满”时,它将读取下一行。。。 例子: 在这种情况下,“muchcourse”是“3”,然后是“查看图像”。第2、3、4行(3行)将插入到其他表中。。因为“多数课程”是“3” 这是我尝试过的编码 def upload = { withForm{
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")
}
}
如果出现muchcourse字段,则字段大小为3,然后在一个表中保存三个数据。否则大小为2,然后将这两个数据保存到另一个表中。我认为这会解决您的问题。@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()
)
}
}
如果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()
如果您试图将课程与学生关联,则可能还需要在迭代器的作用域上方存储对学生的引用,以便将课程与当前正在处理的学生关联。下次看到学生行时,将其存储为当前学生。如果学生与课程关联,则我们可以执行以下操作。def student,course;在if部分,学生=一个表并保存;其他部分创建课程,然后将其添加到学生并保存学生
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()