0
mycursor = mydb.cursor()

sql = "select electricityBalance from users" 
mycursor.execute(sql)
print (int(record[0]) for record in mycursor.fetchone())

but the output gives this <generator object at 0x0000000002CFF2A0>

1
  • try using cursor.fetchall() Commented Jul 5, 2021 at 10:32

1 Answer 1

1

According to this post, a generator expression is a "naked" for expression.

That is why you are getting <generator object at 0x0000000002CFF2A0>:

What you can do is this:

l1=[int(record[0]) for record in mycursor.fetchone()]
print(*l1)

Unpack a list.

You can also use .join method, but keep in mind that it requires elements which are strings.

print(','.join(str(int(record[0])) for record in mycursor.fetchone()))
Sign up to request clarification or add additional context in comments.

6 Comments

it gave me an error saying that "int" object is not subscriptable
Which part? First or second @mostafakhalil
first, the sencond one gave me syntac error unexpected EOF while parsing
@mostafakhalil, check it now?
the second one gave me the first's proplem " int is not subscriptable both same proplem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.