如果重复这一点,我道歉,但我是机器人框架的总菜鸟,我需要帮助。我有两个数组,数据库的结果:
@{users}= query Select * from users
@{fieldNames}= query SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'robot_test' AND TABLE_NAME = 'users';
我从中得到的结果是:
@{users}= (1, 'user1', 'pass1', 'admin'), (2, 'user2', 'pass2', 'user')
@{fieldNames} = [ ('id',) | ('username',) | ('password',) | ('role',) ]
我想过滤@(fieldNames)
成为
@{fieldNames} = [ 'id','username','password','role']
然后将其与@(users)
混合以获取
@{mixedArray}= (('id',1) , ('username','user1'), ('password','pass1'), ('role','admin')), (('id',2) , ('username','user2'), ('password','pass2'), ('role','user'))
有办法做到这一点吗?谢谢大家的帮助。
答案 0 :(得分:2)
因为数据库库将查询结果作为元组列表返回,所以您可以遍历它们。我只做清理就是使用组合列表去除fieldNames列表中的元组。
*** Settings ***
# Library Dialogs
Library Collections
Library DatabaseLibrary
library pymysql
*** Variables ***
@{database} pymysql users root df478444 localhost 3306
*** Test Cases ***
Stackoverflow
Connect To Database @{database}
@{users}= query Select * from Users
@{fieldNames}= query SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'users' AND TABLE_NAME = 'Users';
@{fields list}= Combine Lists @{fieldNames}
@{mixedArray}= Create List
:FOR ${user} IN @{users}
\ ${user row}= parse user ${user} ${fields list}
\ Append To List ${mixedArray} ${user row}
Log ${mixedArray}
*** Keywords ***
Parse User
[Arguments] ${user} ${fields}
@{line}= Create List
${list length}= Get Length ${fields}
:FOR ${i} IN RANGE ${list length}
\ @{pair}= Create List
\ ${f}= Get From List ${fields} ${i}
\ ${u}= Get From List ${user} ${i}
\ Append To List ${pair} ${f}
\ Append To List ${pair} ${u}
\ Append To List ${line} ${pair}
Return From Keyword ${line}