`
MafiaDada
  • 浏览: 25940 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Execute SQL Scripts in Grails Bootstrap or Integration Tests

阅读更多
I recently had to execute some SQL scripts when a Grails applications starts up to insert test data into the database.
Executing sql scripts is easy. Just open the SQL File and read the contents of the Script to a String variable.
String sqlFilePath = 'path/to/your/script.sql'
String sqlString = new File(sqlFilePath).text


Create a SQL connection and use it’s execute(String sql) method to execute your script. To configure the SQL connection, you can use the settings from your DataSource.groovy .
def sql = Sql.newInstance(ConfigurationHolder.config.dataSource.url, 
                  ConfigurationHolder.config.dataSource.username, 
                  ConfigurationHolder.config.dataSource.password,
                  ConfigurationHolder.config.dataSource.driverClassName)
sql.execute(sqlString)


Here is the full sourcecode:
import groovy.sql.Sql
import org.codehaus.groovy.grails.commons.ConfigurationHolder
 
String sqlFilePath = 'path/to/your/script.sql'
String sqlString = new File(sqlFilePath).text
def sql = Sql.newInstance(ConfigurationHolder.config.dataSource.url, 
                   ConfigurationHolder.config.dataSource.username, 
                   ConfigurationHolder.config.dataSource.password, 
                   ConfigurationHolder.config.dataSource.driverClassName)
sql.execute(sqlString)


This can be very useful when you have to insert a lot of test data into the database.


groovy.sql.Sql中的execute()只能执行一行script代码,且不能执行空行,修正后的代码如下:
String sqlFilePath = 'path/to/script.sql'
String sqlString = new File(sqlFilePath).text
def sql = Sql.newInstance(ConfigurationHolder.config.dataSource.url,
                ConfigurationHolder.config.dataSource.username,
                ConfigurationHolder.config.dataSource.password,
                ConfigurationHolder.config.dataSource.driverClassName)
sqlString.eachLine {line -> if(line.trim())sql.execute(line) }



参考文章:
http://blog.oio.de/2010/05/31/execute-sql-scripts-in-grails-bootstrap-or-integration-tests/
http://www.intelligrape.com/blog/2010/09/14/grails-execute-sql-script-in-bootstrap/
http://grails.1312388.n4.nabble.com/Running-a-mysql-dump-from-grails-BootStrap-fails-unexpectedly-td3690734.html
分享到:
评论

相关推荐

    SQL Server EXEC和sp_executesql的区别

    ### SQL Server EXEC与sp_executesql的区别详解 #### 一、引言 在SQL Server中,执行动态SQL或存储过程时,开发人员通常面临选择使用`EXEC`还是`sp_executesql`的问题。这两种方法虽然都能达到目的,但在功能、性能...

    SSIS中ExecuteSQL TASK组件关于参数的使用

    在SSIS(SQL Server Integration Services)中,Execute SQL Task组件是一个强大的工具,允许你在数据流中执行SQL命令或存储过程。这个组件对于ETL(提取、转换、加载)过程至关重要,因为它可以处理数据库中的数据...

    SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别.doc

    在SQL Server中,动态执行SQL语句有两个主要的命令:`EXEC`和`SP_EXECUTESQL`。两者都用于在运行时执行SQL语句,但它们之间存在显著的区别。 一、EXEC `EXEC`命令可以执行存储过程或者动态SQL语句。当我们需要执行...

    ExecuteSQL.java

    ExecuteSQL.java

    hanshu.rar_Execute sql vb

    `ExecuteSql`函数就是这样一个工具,允许程序员运行SQL查询、更新、插入或删除数据。在这个主题中,我们将深入探讨如何在VB中使用`ExecuteSql`函数来操作数据库。 首先,我们需要了解VB中的数据库连接。通常,我们...

    判断execute(sql)执行成功与否

    本文将深入探讨如何有效地判断`execute(sql)`方法执行的结果,并通过具体的示例代码来帮助理解这一过程。 ### 判断`execute(sql)`执行结果的关键概念 #### 1. `execute(sql)`方法简介 `execute(sql)`是数据库连接...

    事务处理函数transaction与executeSQL解析.docx

    本文将深入解析事务处理函数 `transaction` 和执行 SQL 语句的函数 `executeSQL`。 1. **事务处理函数 `transaction`** 事务是数据库操作的基本单元,确保一组操作要么全部成功,要么全部失败,避免了数据不一致的...

    动态SQL 并且把返回的值赋给变量

    本文将详细介绍如何利用`sp_executesql`来执行动态SQL,并重点讨论如何将执行结果赋值给变量,以及一些重要的注意事项。 #### 动态SQL与`sp_executesql` 动态SQL是指在运行时构建的SQL语句,这种类型的SQL可以在不...

    系统存储过程,sp_executesql

    `sp_executesql`是SQL Server中的一个系统存储过程,用于执行可以动态生成或重复使用的Transact-SQL语句和批处理。这个过程对于运行基于输入参数的动态SQL非常有用,能够提高代码的可重用性和安全性,因为它有助于...

    SQL Prompt 4.0, simulate to execute SQL

    You can use this tool to simulate to execute SQL instead of PL-SQL(Oracle) and T-SQL(SQL server). Good SQL practise tool!

    ArabicAtlas_Execute SQL_K_sql_

    "ArabicAtlas_Execute SQL_K_sql_"这个标题可能指的是一个项目或教程,其中包含了与执行SQL查询相关的阿拉伯语资源,特别是针对K系列的SQL操作。 描述中的“test this is just a test for forum goo nack”似乎是一...

    executesql 存储过程

    在SQL Server中,`EXECUTE SQL` 和 `sp_executesql` 是用于执行动态SQL语句的两个关键存储过程。这两个方法在处理不确定或需要在运行时构建的SQL语句时非常有用。以下是对这两个概念的详细解释: 1. **EXECUTE SQL*...

    在sp_executesql中使用like字句的方法

    declare @LikeSql nvarchar(32);–定义一个like变量,如果是存储过程,此处可以存储过程的参数 set @LikeSql = ‘someword%’;...—使用@LikePar变量进行参数化 exec sp_executesql @SelectSql ,N’@LikePa

    SQLServer:探讨EXEC与sp_executesql的区别详解

    在SQL Server中,`EXEC` 和 `sp_executesql` 都是用来动态执行SQL语句的命令,但它们之间存在着显著的区别。这篇文章将详细解析这两个命令的用途、优缺点以及如何选择适合的使用场景。 首先,`EXEC` 命令主要用于...

    html5 Web SQL Database 之事务处理函数transaction与executeSQL解析

    本篇文章将详细解析在Web SQL中进行事务处理的关键函数`transaction`和`executeSQL`。 1. **事务处理函数 `transaction`** `transaction`函数是Web SQL的核心组成部分,它用于执行一系列的数据库操作,确保这些...

    Knight’s 24-Hour Trainer Microsoft SQL Server 2008 Integration Services

    《Knight’s 24-Hour Trainer Microsoft SQL Server 2008 Integration Services》是一本深入讲解如何使用SQL Server 2008 Integration Services (SSIS)进行数据集成的专业指南。本书不仅提供了详尽的安装与启动指导...

    Oracle Database 12c PL-SQL programming

    Execute black box, white box, and integration tests Configure and manage stored packages and libraries Handle security with authentication and encryption Use LOBs to store text and multimedia content ...

    MDBScript注册版-是一个SQL 脚本,让您创建,生成和执行SQL脚本更容易

    是一个SQL 脚本,让您创建,生成和执行SQL脚本更容易,MDBScript is a powerful script generation utility for any MS Access Database (*.mdb, *.accdb).Easily generate SQL scripts for any objects existing in ...

    sqlserver的存储过程与 where in 多值参数

    EXEC sp_executesql @sql END ``` 在这个例子中,`@idList`应该是一个由逗号分隔的值列表,如'1,2,3,4'。调用存储过程时,将这个列表作为参数传递。 **方法二:使用`CHARINDEX`遍历** `CHARINDEX`函数可以用来...

Global site tag (gtag.js) - Google Analytics