Using string replace() to get a value from a DB query result leads to problems

自闭症网瘾萝莉.ら 提交于 2019-12-11 17:53:36

问题


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?

I tried using Evaluate function:

Evaluate   '(('ABC',),)'.replace('(',' ')   

I need result as just ABC.


回答1:


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.

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]

If for example the query returnes 3 rows, each with 2 columns:

[('ABC', 'DEF'), ('GHI', 'JKL'), ('MNO', '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.



来源:https://stackoverflow.com/questions/54296712/using-string-replace-to-get-a-value-from-a-db-query-result-leads-to-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!