Home > Blockchain >  Issue with Python fetch all parsing
Issue with Python fetch all parsing

Time:05-05

In my python app getting data from DB by the below query,

cur = connM.cursor()
cur.execute("SELECT * FROM users WHERE useremail = '{}' ".format(userEmail)) 
user_record = cur.fetchall()

and from the response user_record, I am trying to get the only value which is in index 3 like below ;

value = [item[2] for item in user_record]

in user_record, I have the below value when I print it,

[(3, '[email protected]', '00000000', 'Test', 'Test', '1234', '123456')]

however in value when I am printing it I am seeing below,

['00000000']

how I can assign a pure string value to the variable so I can use it after that in some conditions like;

if value == "00000000":
..
..

CodePudding user response:

fetchall returns a list of tuples of query result. To get the third element and also to do the comparison you can either iterate over the user_record and access the 2nd index.

for item in user_record:
    if item[2] == "00000000":
        # TODO

or If you are sure that the result will always contains single element, you can directly index the element,

if user_record[0][2] == "00000000":
    pass
  • Related