【问题标题】:groovy array remove the bracketsgroovy array 删除括号
【发布时间】:2015-01-21 09:56:03
【问题描述】:
  def colarray=[]
                    def num
                    def newRole = rolecol.split(',')
                    def len = newRole.size()
                    println "$newRole,$len"
                    for (num = 0; num < len; num++) {
                        def col = "col"
                        col="$col"+newRole[num]
                        colarray.add(col)

                    }
                    println colarray
                    sql.eachRow("select col01,$colarray from read_csv where col01=? and col${usercol}!=? ", [file.name,""])

我想将 col1..col11 保存到数组中并从 select 语句中调用它,但问题是 $colarray 带有括号(如 [col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33] ),所以现在我想删除它们,任何人都可以帮助它???thx

【问题讨论】:

    标签: list groovy brackets


    【解决方案1】:

    [] 在那里,因为您在列表中执行隐式toString。使用colarray.join(',')

    【讨论】:

    • def aa=colarray.join(',') println aa // sql.eachRow("select col01,$aa from read_csv where col01=? and col${usercol}!=? ", [file.name,""]) 您好,我是这样编辑的,但出现错误,请查看以下评论​​
    【解决方案2】:
    def colarray = rolecol.tokenize(',').collect { "col$it" }.join(', ')
    

    那么你还需要转义你的sql:

    sql.eachRow("select col01, ${Sql.expand(colarray)} from read_csv where col01 = ? and ${Sql.expand('col' + usercol)} != ? ", [file.name,""])
    

    【讨论】:

    • 嗨,我试过了,但我的代码得到了同样的错误信息。请检查我的答案。如果你能再次帮助谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2023-04-10
    • 2019-08-11
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多