Python mysql駆動mysql.connector fetchone(),fetchall(),fetchmany()
6181 ワード
def fetchone(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
return None
def fetchmany(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 > 0 and self._have_unread_result():
cnt -= 1
row = self.fetchone()
if row:
res.append(row)
return res
def fetchall(self):
"""
Returns all rows of a query result set
Returns a list of tuples.
"""
if not 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 >= 0 and self._rowcount == -1:
self._rowcount = 0
self._rowcount += rowcount
return rows
見える
cursor.fetchall() [()] [] fetchone
, raise errors.InterfaceError("No result set to fetch from.") mysql.connector fetcall()
cursor.fetchmany(size=1) [()] , [] size fetchone
cursor.fetchone() () None
MySQLdb , MySQLdb fectchall()