[英]Why is tuple being returned?
I have the following: 我有以下内容:
tableNumber = session.query(TABLE.TABLESNUMBER).filter_by(TABLESID=self.TABLESID).first()
return str(tableNumber)
This is my TABLE
class: 这是我的TABLE
类:
class TABLE(Base):
....
TABLESID = Column(Integer, primary_key=True)
TABLESNUMBER = Column(Integer, nullable=False)
...
This is the output I am receiving: 这是我收到的输出:
(1L,)
In my mySQL database
, all both values above are represented as ints
. 在我的mySQL database
,以上所有两个值都表示为ints
。
I would just like 1
to be returned. 我只想退回1
。
query().first()
returns the first row. query().first()
返回第一行。 That's why a tuple is being returned. 这就是返回元组的原因。 The query does not care that in the first row there is only 1 value, resulting in a tuple of length 1. 该查询并不关心第一行中只有1个值,从而得出长度为1的元组。
If you're sure the query will return only 1 scalar value, you can use scalar()
instead of first()
. 如果确定查询仅返回1个标量值,则可以使用scalar()
而不是first()
。 Please read up the linked documentation. 请阅读链接的文档。
edit: To get the first value in the first returned tuple, when the query may return multiple rows, you can either add order_by(...).limit(1)
to the query and then use scalar()
, or get the first row and and then explicitly convert the tuple to an int. 编辑:要获取第一个返回的元组中的第一个值,当查询可能返回多行时,可以将order_by(...).limit(1)
到查询中,然后使用scalar()
,或获取第一个行,然后将元组显式转换为int。
first() and all() return 'tuple' first()和all()返回“元组”
So, 所以,
session.query(TABLE.TABLESNUMBER).filter_by(TABLESID=self.TABLESID).first()
returns its tuple including 'TABLESNUMBER' session.query(TABLE.TABLESNUMBER).filter_by(TABLESID=self.TABLESID).first()
返回其元组,包括“ TABLESNUMBER”
If you want exactly one value (TABLESNUMBER) 如果您只想要一个值(TABLESNUMBER)
You can use it. 您可以使用它。 session.query(TABLE.TABLESNUMBER).filter_by(TABLESID=self.TABLESID).first().TABLESNUMBER
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.