我试图用pyodbc打印前10行。我知道如何使用下得到的第一条记录:Pyodbc - 打印第10行(python)
row = cursor.fetchall()
我试图将此更改为:
row = cursor.fetchten()
但这并没有工作。还有什么我能做的吗?
我试图用pyodbc打印前10行。我知道如何使用下得到的第一条记录:Pyodbc - 打印第10行(python)
row = cursor.fetchall()
我试图将此更改为:
row = cursor.fetchten()
但这并没有工作。还有什么我能做的吗?
您插入:
row = cursor.fetchmany(10)
您可以更改括号任何你想要的号码。
根据找到的文档on this page,您有两种选择来返回列表。您有fetchall()
方法和fetchmany()
方法。无论哪种情况,您都会返回一个要处理的行列表。
关于fetchall()
方法和捎带过什么zondo说,以下工作快速,高效地:
rows = cursor.fetchall()[:10] # to get the first 10
rows = cursor.fetchall()[-10::1] # to get the last 10
或者,你可以多次循环遍历行,你需要得到你所需要的结果:
rows = cursor.fetchall()
for idx in range(10): #[0, 1, ..., 9,]
print(rows[idx]) # to get the first 10
print(rows[(len(ray)-idx)]) # to get the last 10
还有同一文档中的fetchmany()
方法,定义如下:cursor.fetchmany([size=cursor.arraysize]) --> list
括号表示可选参数,所以您不需要包含大小。但既然你想要10,你会传入10的大小参数。例如:
rows = cursor.fetchmany(size=10)
for row in rows:
print(row)
'fetchall'将返回光标所有行,而不仅仅是第一行。你究竟在做什么? –
我对pyodbc没有做任何事情,所以这可能是完全的乱码,但是你不能做'cursor.fetchall()[:10]'? – zondo