how to take data from csv file and save into grails with mysql?
csv, grails, groovy, mysql
Solution
You need to get the InputStream from the MultipartFile you are passed as shown in the documentation:
<g:uploadForm action="upload">
<input type="file" name="filecsv" />
<input type="submit" />
</g:uploadForm>
Then;
def upload = {
request.getFile( 'filecsv' )
.inputStream
.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()}")
}
}
Problem
example : i have a CSV file like this and i want it save in to database..with upload the CSV's files. this is my coding for upload CSV file ``` <input type="file" name="filecsv"/> <input type="button" class="upload" value="Upload onclick='location.href ="${createLink(url: [action: 'upload'])}"'/> ``` i confuse in groovy..i tried like this code but not success. ``` 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 how to get data from file CSV and save it into database mysql?