<i id='UUphw'><tr id='UUphw'><dt id='UUphw'><q id='UUphw'><span id='UUphw'><b id='UUphw'><form id='UUphw'><ins id='UUphw'></ins><ul id='UUphw'></ul><sub id='UUphw'></sub></form><legend id='UUphw'></legend><bdo id='UUphw'><pre id='UUphw'><center id='UUphw'></center></pre></bdo></b><th id='UUphw'></th></span></q></dt></tr></i><div id='UUphw'><tfoot id='UUphw'></tfoot><dl id='UUphw'><fieldset id='UUphw'></fieldset></dl></div>
      <tfoot id='UUphw'></tfoot>

      <small id='UUphw'></small><noframes id='UUphw'>

      <legend id='UUphw'><style id='UUphw'><dir id='UUphw'><q id='UUphw'></q></dir></style></legend>
        • <bdo id='UUphw'></bdo><ul id='UUphw'></ul>

        GroovySql:如何使用 Arraylist 变量更新表

        时间:2023-08-19
          <tfoot id='Ufb27'></tfoot>
            <bdo id='Ufb27'></bdo><ul id='Ufb27'></ul>
            1. <small id='Ufb27'></small><noframes id='Ufb27'>

                <i id='Ufb27'><tr id='Ufb27'><dt id='Ufb27'><q id='Ufb27'><span id='Ufb27'><b id='Ufb27'><form id='Ufb27'><ins id='Ufb27'></ins><ul id='Ufb27'></ul><sub id='Ufb27'></sub></form><legend id='Ufb27'></legend><bdo id='Ufb27'><pre id='Ufb27'><center id='Ufb27'></center></pre></bdo></b><th id='Ufb27'></th></span></q></dt></tr></i><div id='Ufb27'><tfoot id='Ufb27'></tfoot><dl id='Ufb27'><fieldset id='Ufb27'></fieldset></dl></div>
                    <tbody id='Ufb27'></tbody>
                • <legend id='Ufb27'><style id='Ufb27'><dir id='Ufb27'><q id='Ufb27'></q></dir></style></legend>
                • 本文介绍了GroovySql:如何使用 Arraylist 变量更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个具有三个不同 Arraylist 变量的 GroovySQL 脚本.A1[1,2,3],A2[4,5,6],A3[7,8,9].

                  I am trying to write an GroovySQL script that will have three different Arraylist variable. A1[1,2,3],A2[4,5,6],A3[7,8,9].

                  我想更新表格,使表格三列的三行更新为
                  数据应该是(按行)
                  R1:1,4,7
                  R2:2,5,8
                  R3:3,6,9

                  I want to update the table such that three rows of the three columns of the table are updates as
                  Data should be (in row wise)
                  R1: 1,4,7
                  R2: 2,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])
                  

                  我知道如何更新一行.如果有人可以提供任何提示或想法,我将不胜感激.

                  I managed know how to update one row. I will be thankful if anyone can give any hints or ideas.

                  推荐答案

                  你需要做的就是创建一个二维数组并转置它,然后通过它循环执行更新查询.

                  All you need to do is create a 2d array and transpose it, then execute the update query by loop thru it.

                  这是脚本:

                  //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)
                  }
                  

                  您可以在下面看到查​​询是如何构建的:

                  You may see how the query is build in below:

                  感谢 tim_yates 的 转置矩阵

                  这篇关于GroovySql:如何使用 Arraylist 变量更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何为 WinRT/ARM 编译 sqlite? 下一篇:如何将 Byte[] 插入 SQL Server VARBINARY 列

                  相关文章

                  最新文章

                    <legend id='gv5jN'><style id='gv5jN'><dir id='gv5jN'><q id='gv5jN'></q></dir></style></legend>

                  1. <small id='gv5jN'></small><noframes id='gv5jN'>

                      <tfoot id='gv5jN'></tfoot>

                        <bdo id='gv5jN'></bdo><ul id='gv5jN'></ul>
                      <i id='gv5jN'><tr id='gv5jN'><dt id='gv5jN'><q id='gv5jN'><span id='gv5jN'><b id='gv5jN'><form id='gv5jN'><ins id='gv5jN'></ins><ul id='gv5jN'></ul><sub id='gv5jN'></sub></form><legend id='gv5jN'></legend><bdo id='gv5jN'><pre id='gv5jN'><center id='gv5jN'></center></pre></bdo></b><th id='gv5jN'></th></span></q></dt></tr></i><div id='gv5jN'><tfoot id='gv5jN'></tfoot><dl id='gv5jN'><fieldset id='gv5jN'></fieldset></dl></div>