Parse CSV and export into Mysql database in Grails(Grails中解析CSV并导出到Mysql数据库)
问题描述
我是 Groovy & 的新手圣杯.我想提交解析 CSV 文件并导出到 MySQL 数据库的几个表中.我看过一些编码,但作为新手,这让我感到困惑.那么任何人都可以帮助我理解简单的 csv 文件解析并导出到 MySQL 数据库中.
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.
谢谢索努
推荐答案
Grails 一个引导程序,它在您的应用程序启动时运行.它的漂亮;你可以配置它在不同的环境中做不同的事情.
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) 读取 csv 文件,随时创建域对象.
2)对于每个域对象,检查它是否存在,如果不存在你DomainObject.save()
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()
就是这样.
对于代码,类似
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()}")
}
这篇关于Grails中解析CSV并导出到Mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Grails中解析CSV并导出到Mysql数据库
基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01