Ale*_*x Q 64 python mysql python-db-api
我今天刚与一些同事就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)
Dav*_*eau 14
我认为这确实取决于实现,但您可以通过查看MySQLdb源来了解差异.根据选项,mysqldb fetch*保留内存或服务器端的当前行集,因此fetchmany vs fetchone在这里有一些灵活性,可以知道在(python)内存中保留什么以及保持数据库服务器端的内容.
PEP 249没有提供太多细节,所以我想这是根据数据库优化的东西,而确切的语义是实现定义的.
这些是特定于实现的.
将从表中获得所有结果.当表的大小很小时,这将更好地工作.如果表大小较大,则在这些情况下fetchall将失败.
将使用大部分内存.
如果在网络上完成查询,将导致一些问题.
fetchmany将只获得所需数量的结果.您可以产生结果和过程.fetchmany实现的简单片段.
while True:
results = cursor.fetchmany(arraysize)
if not results:
break
for result in results:
yield result
Run Code Online (Sandbox Code Playgroud)
fetchone()
Fetch the next row of a query result set, returning a single tuple, or None when no more data is available:
>>> cur.execute("SELECT * FROM test WHERE id = %s", (3,))
>>> cur.fetchone()
(3, 42, 'bar')
Run Code Online (Sandbox Code Playgroud)
A ProgrammingError is raised if the previous call to execute*() did not produce any result set or no call was issued yet.
fetchmany([size=cursor.arraysize])
Fetch the next set of rows of a query result, returning a list of tuples. An empty list is returned when no more rows are available.
The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor’s arraysize determines the number of rows to be fetched. The method should try to fetch as many rows as indicated by the size parameter. If this is not possible due to the specified number of rows not being available, fewer rows may be returned:
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchmany(2)
[(1, 100, "abc'def"), (2, None, 'dada')]
>>> cur.fetchmany(2)
[(3, 42, 'bar')]
>>> cur.fetchmany(2)
[]
Run Code Online (Sandbox Code Playgroud)
A ProgrammingError is raised if the previous call to execute*() did not produce any result set or no call was issued yet.
Note there are performance considerations involved with the size parameter. For optimal performance, it is usually best to use the arraysize attribute. If the size parameter is used, then it is best for it to retain the same value from one fetchmany() call to the next.
List item
fetchall()
Fetch all (remaining) rows of a query result, returning them as a list of tuples. An empty list is returned if there is no more record to fetch.
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchall()
[(1, 100, "abc'def"), (2, None, 'dada'), (3, 42, 'bar')]
Run Code Online (Sandbox Code Playgroud)
A ProgrammingError is raised if the previous call to execute*() did not produce any result set or no call was issued yet.
归档时间: |
|
查看次数: |
42813 次 |
最近记录: |