Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query which on realtime will give 4million + record, i want to pass the query output one by one in to the for loop and want to do the function inside the for loop. The out put of the query will be like
[(167806,), (167806,), (50791734,), (7615365,), (641219,), (6990522,), (641219,), (641219,), (4943654,), (4943654,)]

i want to pass it one by one in the inside the for loop .

What I have tried:

query = "select  id from contacts limit 10"
    print (query)
    cursor.execute(query)
    contact = cursor.fetchall()
    print(contact)
    id = contact[0][0]
    print(id)
    for row in contact:
        print (id)
        ivurl = f'{api}/company/{id}/contacts?'
        payload: Dict[str, Union[bool, int]] = {'isEmailRequired': True}
        response = get_iv_response(url=ivurl, payload=payload)
        email_contact_totalresult: int = response.get('totalResults')
        email = int(email_contact_totalresult)
        if email > 0:
            print("true")
        else :
            print ("false") 
Posted
Updated 15-Oct-20 22:03pm
Comments
CHill60 15-Oct-20 12:40pm    
So what happens when you run your code?
Richard MacCutchan 15-Oct-20 12:41pm    
:snap:
Member 13998042 15-Oct-20 12:44pm    
https://ibb.co/n3P8wjr
Member 13998042 15-Oct-20 12:42pm    
only the first value from the query keeps on repeating inside the loop
Member 13998042 15-Oct-20 12:45pm    
https://ibb.co/n3P8wjr
the first value of the query keeps on repeating

1 solution

Quote:
Python
id = contact[0][0]
print(id)
for row in contact:
    print (id)
only the first value from the query keeps on repeating inside the loop
Look at your code again. You are reading the first ID outside of the loop, and printing the same value over and over again.

Move the id assignment inside the loop.
Python
for row in contact:
    id = row[0]
    print (id)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900