deffetchone(self):"""Returns next row of a query result set
Returns a tuple or None.
"""
row = self._fetch_row()
if row:
if hasattr(self._connection, 'converter'):
return self._connection.converter.row_to_python(
row, self.description)
return row
returnNonedeffetchmany(self, size=None):"""
Returns the next set of rows of a query result, returning a
list of tuples. When no more rows are available, it returns an
empty list.
The number of rows returned can be specified using the size argument,
which defaults to one
"""
res = []
cnt = (size or self.arraysize)
while cnt > 0and self._have_unread_result():
cnt -= 1
row = self.fetchone()
if row:
res.append(row)
return res
deffetchall(self):"""
Returns all rows of a query result set
Returns a list of tuples.
"""ifnot self._have_unread_result():
raise errors.InterfaceError("No result set to fetch from.")
(rows, eof) = self._connection.get_rows()
if self._nextrow[0]:
rows.insert(0, self._nextrow[0])
if hasattr(self._connection, 'converter'):
row_to_python = self._connection.converter.row_to_python
rows = [row_to_python(row, self.description) for row in rows]
self._handle_eof(eof)
rowcount = len(rows)
if rowcount >= 0and self._rowcount == -1:
self._rowcount = 0
self._rowcount += rowcount
return rows
可以看到
cursor.fetchall() 返回[()] 或者[] 使用了fetchone
若是没有数据,则会抛出异常 raiseerrors.InterfaceError("No result set to fetch from.") 所以 mysql.connector的fetcall()不能处理空数据
cursor.fetchmany(size=1) [()] ,或者[] 指定size 返回多行 fetchonecursor.fetchone() () 或者None