使用字符串替换()从数据库查询结果中获取值会导致问题 [英] Using string replace() to get a value from a DB query result leads to problems
问题描述
我正在查询数据库并想验证我的输出.来自 DB 的结果以 -> (('ABC',),)
的方式出现,我想用字符串 'ABC' 验证它,但由于在我的数据库输出.
I am querying DB and want to validation my output. The result from DB is coming in the manner -> (('ABC',),)
and I want to validate it with string 'ABC', but I am unable due to extra characters received in my db output .
有人能帮我删除从数据库输出的额外字符吗?
Can anybody help me with how to remove extra characters that are coming as output from DB?
我尝试使用评估功能:
Evaluate '(('ABC',),)'.replace('(',' ')
我只需要 ABC 的结果.
I need result as just ABC.
推荐答案
不要那样做 - 将响应视为字符串,并尝试通过字符串替换来获取数据.
Do not do that - treat the response as a string, and try to get you data out through string replace.
响应是一个对象 - 一个元组列表,它实际上是这样的:
The response is an object - a list of tuples, it actually looks like this:
[('ABC',),]
列表中的每个元组都是一个响应行;元组的每个成员都是该行中的一列.
Every tuple in the list is a response row; every member of the tuple is a column in that row.
要获得第一行的第一列,您只需寻址它们(它们的索引从 0 开始):
To get the first column of the first row, you just address them (their indices start from 0):
${value}= Set Variable ${the response object}[0][0]
<小时>
例如,如果查询返回 3 行,每行 2 列:
If for example the query returnes 3 rows, each with 2 columns:
[('ABC', 'DEF'), ('GHI', 'JKL'), ('MNO', 'PQR')]
,你会得到第三行(索引:2)第二列(索引:1) - 字符串'PQR' - 用这个:
, you'd get the 3rd row's (index: 2) 2nd column (index: 1) - the string 'PQR' - with this:
${value}= Set Variable ${the response object}[2][1]
现在我希望你明白为什么使用字符串替换(在二维列表的字符串表示上)不是一个好主意.
Now I hope you understand why using string replace (over the string representation of a two-dimensional list) is not a good idea.
这篇关于使用字符串替换()从数据库查询结果中获取值会导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!