解析CSV并导出到Grails中的Mysql数据库中 [英] Parse CSV and export into Mysql database in Grails

查看:116
本文介绍了解析CSV并导出到Grails中的Mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Groovy& Grails的。我想提交解析CSV文件并导出到MySQL数据库的几个表中。
我看了一些编码,但作为新手我感到困惑。
所以任何人都可以帮助我理解简单的csv文件解析并导出到MySQL数据库中。



感谢
Sonu


Grails每当您的应用程序启动时都会运行一个引导程序进程。它的漂亮;您可以将其配置为在不同的环境中执行不同的操作。



1)读取csv文件,创建Domain对象随你去。

2)对于每个域对象,检查它是否存在,如果不存在,你做DomainObject.save()

那就是它。



代码,类似于

 新文件(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})
}
$ b log.debug(Importing domainObject $ {domainObject.toString()})
}


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

解决方案

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()}")
}

这篇关于解析CSV并导出到Grails中的Mysql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆