2017-02-17 1 views
1

세 가지 Arraylist 변수가있는 GroovySQL 스크립트를 작성하려고합니다. A1 [1,2,3], A2 [4,5,6], A3 [7,8,9]. 1,4,7
R2 : 2GroovySql : Arraylist 변수를 사용하여 테이블을 업데이트하는 방법

는 I 테이블의 세 열 세 행 (현명한 행)이어야
데이터로 업데이트
R1이되도록 상기 테이블을 업데이트 할 , 5,8
R3 : 3,6,9 내가 한 행을 업데이트하는 방법을 알고 관리

def sql = Sql.newInstance("jdbc:mysql://localhost:3306/words", "Test", 
      "test", "com.mysql.jdbc.Driver") 
def nid = 1 
def newupdate = "hello world" 
sql.executeUpdate("update word set spelling = ? where word_id = ?", [ newupdate, nid]) 

. 누구든지 힌트 나 아이디어를 줄 수 있다면 감사 할 것입니다.

+0

에 대한 tim_yates

enter image description here

크레딧? 'groovy'와 'groovy-sql'을 사용하는 것이 좋습니다. – Axel

답변

1

당신이해야 할 일은 2 차원 배열을 만들고 그것을 조 변경 한 다음 루프를 통해 업데이트 쿼리를 실행하기 만하면됩니다. 여기

는 스크립트입니다

//Defined the data that you mentioned 
def A1 = [1,2,3] 
def A2 = [4,5,6] 
def A3 = [7,8,9] 

//Chage here your column names that you want to update 
//column_0 can be your in your where clause 
def columnNames = ['column_0', 'column_1', 'column_2'] 

//2d array of above data 
def matrix = [A1, A2, A3] 

//Transpose it to change rows & columns 
def transMatrix = (0..<(matrix*.size().max())).collect { 
    matrix*.getAt(it) 
} 
println "Transposed matrix is ==> $transMatrix" 



def sql = Sql.newInstance("jdbc:mysql://localhost:3306/words", "Test", "test", "com.mysql.jdbc.Driver") 

//Loop thru transposed matrix, 
//Build the query 
//Pass it to executeUpdte 
transMatrix.each { rowData -> 
    def query = "update word set ${columnNames[1]} = '${rowData[1]}', ${columnNames[2]} = '${rowData[2]}' where ${columnNames[0]} = '${rowData[0]}'" 
    println "Generated query is : ${query}" 
    sql.executeUpdate(query) 
} 

당신은 쿼리가 아래에 구축 얼마나 볼 수 있습니다 :이 자바 태그 왜 transpose matrix

관련 문제