Skip to content

Instantly share code, notes, and snippets.

@atonse
Forked from rynmrtn/insert.groovy
Created January 22, 2010 21:13
Show Gist options
  • Save atonse/284147 to your computer and use it in GitHub Desktop.
Save atonse/284147 to your computer and use it in GitHub Desktop.
/**
This groovy script will take a CSV file that represents the data
in a database. The first row should be the name of the columns and
all other rows should represent the data you wish to insert.
*/
// Setup basic information
numColumns = 6
tableName = "TABLE_NAME"
fileName = "C:\\file\\path"
i = 0
new File(fileName).splitEachLine(',') { fields ->
if(i++ == 0) {
columns = "(${fields.join(',')})"
} else {
values = fields.collect { it.equals("sysdate") ? it : ("'" + it + "'") }
println "INSERT INTO ${tableName}${columns} VALUES (${values.join(',')});"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment