sqlalchemy 打印结果而不是对象 [英] sqlalchemy print results instead of objects

查看:40
本文介绍了sqlalchemy 打印结果而不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将查询结果打印到控制台,但它一直向我返回对象位置.

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)

我得到的结果是 -

如何打印value 而不是对象?

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

我问的原因是因为我想使用该值进行比较测试.EG:

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

推荐答案

像这样,test 包含查询返回的所有行.

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

如果你想要一些可以迭代的东西,你可以使用 fetchall 例如.像这样:

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

然后您可以遍历行列表.每行将包含所有字段.在您的示例中,您可以按位置访问字段.你只有一个在位置一.这就是您可以访问 1 的方法:

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

这篇关于sqlalchemy 打印结果而不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆