Python mysql驱动 mysql.connector fetchone(),fetchall(),fetchmany()

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.connectorfetcall()不能处理空数据
cursor.fetchmany(size=1) [()] ,或者[] 指定size 返回多行  fetchone
cursor.fetchone()  () 或者None
而我们平常使用的MySQLdb三个方法也类似,不过MySQLdb的fectchall()可以处理空数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoLiuRs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值