我正在尝试遍历表数据并将表记录保存到Groovy Map中,但只有最后一条记录保存在地图中。
服务代码
#:kivy 1.10.0
<CustButton@Button>:
font_size: 32
color: 0, 0, 0, 1
size: 150, 50
background_normal: ''
background_down: 'bt-down.png'
background_color: .88, .88, .88, 1
<CustomWidget>:
font_size: 32
color: 0, 0, 0, 1
size: 150, 150
background_color: 0.88, 0.88, 0.88, 1
CustButton:
text: "Random"
pos: root.x, 200
CustButton:
text: "Buttom"
pos: 200, root.y
CustButton:
text: "Buttom"
pos: 200, 400
以下员工的表记录:
这是完成迭代后Map的含义:
答案 0 :(得分:2)
试试这个
def list = []
int count = 0
sql.eachRow("SELECT * FROM employee") { row ->
def tableMap = [:]
tableMap.'first_name' = row.first_name
tableMap.'last_name' = row.last_name
tableMap.'born' = row.born
list << tableMap
print "\nIteration No " + count
count++
}
list.each {
for (def e in it) {
print "key = ${e.key}, value = ${e.value}"
}
}
您需要创建一个列表来存储所有地图,否则地图中的旧值将被新值替换。