Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy print results instead of objects

I am trying to print the results of my query to the console, with the below code, but it keeps returning me the object location instead.

test = connection.execute('SELECT EXISTS(SELECT 1 FROM "my_table" WHERE Code = 08001)')
print(test)

Result that I get is - <sqlalchemy.engine.result.ResultProxy object at 0x000002108508B278>

How do I print out the value instead of the object?

Reason I am asking is because I want to use the value to do comparison tests. EG:

if each_item not in test:
    # do stuffs
like image 347
jake wong Avatar asked Feb 07 '17 15:02

jake wong


Video Answer


1 Answers

Like this, test contains all the rows returned by your query.

If you want something you can iterate over, you can use fetchall for example. Like this:

test = connection.execute('SELECT EXISTS(SELECT 1 FROM "my_table" WHERE Code = 08001)').fetchall()

Then you can iterate over a list of rows. Each row will contain all the fields. In your example you can access fields by their position. You only have one at position one. So that's how you can access 1:

for row in test:
    print(row[0])
like image 200
sandor Avatar answered Sep 17 '22 12:09

sandor