2

I am newbie in Groovy & Grails. I want to submit parse CSV file and export into several tables of MySQL database. I have looked some coding but it was confusing for me as newbie. So can anybody help me in understanding simple csv file Parsing and exporting into MySQL database.

Thanks Sonu

2 Answers 2

11

Grails a bootstrap process that runs whenever your app starts. Its nifty; you can configure it to do different things in different environments.

One approach is to do the following in bootstrap:

1) Read the csv file, creating Domain objects as you go.
2) For each domain object, check to see if it exists, and if not do youDomainObject.save()

thats it.

for code, something like

new File(filePath).splitEachLine(',') {fields ->
    def domainObject = new YouDomainObject(
        id: fields[0].trim(),
        name: fields[1].trim()
    )

    if (domainObject.hasErrors() || domainObject.save(flush: true) == null) {
        log.error("Could not import domainObject  ${domainObject.errors}")
    }

    log.debug("Importing domainObject  ${domainObject.toString()}")
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since Groovy integrates with Java you can also use a Java library called opencsv to read the CSV if you're more comfortable with that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.