我今天刚刚和一些同事讨论了 python 的 db-api fetchone vs fetchmany vs fetchall。
我确定每个用例都取决于我正在使用的 db-api 的实现,但总的来说,fetchone、fetchmany 和 fetchall 的用例是什么?
换句话说,以下是等价的吗?还是其中有一个比其他的更受欢迎?如果是这样,在哪些情况下?
cursor.execute("SELECT id, name FROM `table`")
for i in xrange(cursor.rowcount):
id, name = cursor.fetchone()
print id, name
cursor.execute("SELECT id, name FROM `table`")
result = cursor.fetchmany()
while result:
for id, name in result:
print id, name
result = cursor.fetchmany()
cursor.execute("SELECT id, name FROM `table`")
for id, name in cursor.fetchall():
print id, name