【发布时间】:2014-02-17 23:19:22
【问题描述】:
示例: 我有一个这样的 CSV 文件
我希望它保存到数据库中..上传 CSV 文件。
这是我上传 CSV 文件的编码
<input type="file" name="filecsv"/>
<input type="button" class="upload" value="Upload
onclick='location.href ="${createLink(url: [action: 'upload'])}"'/>
我在 groovy 中感到困惑..我尝试过这样的代码,但没有成功。
def upload = {
println params.filecsv
new File('filecsv').splitEachLine(',') {fields ->
def city = new City(
city: fields[0].trim(),
description: fields[1].trim()
)
if (city.hasErrors() || city.save(flush: true) == null) {
log.error("Could not import domainObject ${city.errors}")
}
log.debug("Importing domainObject ${city.toString()}")
}
Parse CSV and export into Mysql database in Grails
如何从 CSV 文件中获取数据并保存到数据库 mysql 中?
【问题讨论】:
-
另外,"...but not success" 是什么意思?你收到错误了吗?
-
我已经编辑了我的帖子,我无法获取此路径 "" 但如果 new File('C:\\user \\desktop\\book1.csv').splitEachLine(',') {fields ->