sqlalchemy print results instead of objects

前端 未结 2 1229
醉酒成梦
醉酒成梦 2021-01-12 21:20

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.e         


        
相关标签:
2条回答
  • 2021-01-12 21:56

    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])
    
    0 讨论(0)
  • 2021-01-12 22:00

    test is an object containing the rows values. So if the column's name is value, you can call it using test.value.

    If you're looking for more "convenient" way of doing so (like iterating through each column of test), you'd have to explicitly define these functions (either as methods of test or as other functions designed to iterate through those types of rows).

    0 讨论(0)
提交回复
热议问题