mostafa khalil
mostafa khalil

Reputation: 1

how do i extract integer values in fetching data from mysql database using python

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>

Upvotes: 0

Views: 314

Answers (1)

user15801675
user15801675

Reputation:

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()))

Upvotes: 1

Related Questions