LoginSignup
4
2

More than 5 years have passed since last update.

フォルダ内のSQLファイルを全て実行する

Last updated at Posted at 2012-10-29

タイトルの件を行う必要があったのでとりあえず作成。
もう少し綺麗な書き方があるはず。

import groovy.sql.Sql

println "start".center(30,"-")

def sql = Sql.newInstance("jdbc:mysql://localhost:3309/test", "yamap", "yamap", "com.mysql.jdbc.Driver")
def folder = /C:\work\sql/
new File(folder).eachFile{
  // 拡張子でsqlファイルを判別
  if (!it.name.split("\\.").last().equals("sql")) {
    return
  }
  try {
    // ファイル内を見てselectかどうか判別
    if (it.text.substring(0,10).contains("select")) {
      sql.rows(it.text)
    } else {
      sql.executeUpdate(it.text)
    }
  } catch(e) {
    println "エラー発生".center(30,"-")
    println it
    println e
    println "-"*40
  }
}
println "end".center(30,"-")
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2