为什么 cursor.with_rows return `True` 但 fetch_one return `None`?
Why would cursor.with_rows return `True` but fetch_one return `None`?
鉴于此部分功能(和 Python 2.7,mysql-连接器 2.1.6):
def get_target_item(self, query, key):
self._target_query.execute(query, (key,))
if self._target_query.with_rows:
raw_item = self._target_query.fetchone()
我原以为raw_item总是非None
,但结果有时是None
。
如果我添加这个:
if not raw_item:
print "Could not find find target item for query {0} and key {1}".format( query, key )
我可以看到它有时会被打印出来。如果我自己 运行 相同的查询,我可以看到查询没有 return 指定键的行。那么为什么 with_rows
暗示它确实如此呢?
以下是 .with_rows
的文档内容:
Returns whether the cursor could have rows returned
This property returns True when column descriptions are available and
possibly also rows, which will need to be fetched.
可以看到,只要游标的description
属性不是None
,with_rows
就会returnTrue
.
如果您使用的是缓冲游标,您可以检查游标的 rowcount
属性以确保它大于 0
。这应该构成更好的检查。
希望这有用。
鉴于此部分功能(和 Python 2.7,mysql-连接器 2.1.6):
def get_target_item(self, query, key):
self._target_query.execute(query, (key,))
if self._target_query.with_rows:
raw_item = self._target_query.fetchone()
我原以为raw_item总是非None
,但结果有时是None
。
如果我添加这个:
if not raw_item:
print "Could not find find target item for query {0} and key {1}".format( query, key )
我可以看到它有时会被打印出来。如果我自己 运行 相同的查询,我可以看到查询没有 return 指定键的行。那么为什么 with_rows
暗示它确实如此呢?
以下是 .with_rows
的文档内容:
Returns whether the cursor could have rows returned
This property returns True when column descriptions are available and possibly also rows, which will need to be fetched.
可以看到,只要游标的description
属性不是None
,with_rows
就会returnTrue
.
如果您使用的是缓冲游标,您可以检查游标的 rowcount
属性以确保它大于 0
。这应该构成更好的检查。
希望这有用。