相关疑难解决方法(0)

Python db-api:fetchone vs fetchmany vs fetchall

我今天刚与一些同事就python的db-api fetchone vs fetchmany vs fetchall进行了讨论.

我确定每个用例的用例取决于我正在使用的db-api的实现,但一般来说fetchone vs fetchmany vs 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
Run Code Online (Sandbox Code Playgroud)

python mysql python-db-api

64
推荐指数
3
解决办法
4万
查看次数

标签 统计

mysql ×1

python ×1

python-db-api ×1