查询通常有两种方式:一种是使用cursor.fetchall()获取所有查询结果,然后再一行一行的迭代;另一种每次通过cursor.fetchone()获取一条记录,直到获取的结果为空为止。通过如下方式来验证这两种查询方式的区别:
TEST表内容如下:
(1, 'a', 'b', 'c')
(2, 'aa', 'bb', 'cc')
(3, 'aaa', 'bbb', 'ccc')
输出结果:
1, a, b, c
Number of rows returned: 3
2, aa, bb, cc
Number of rows returned: 3
3, aaa, bbb, ccc
Number of rows returned: 3
Number of rows returned: 3
Number of rows returned: 4
1, a, b, c
Number of rows returned: 5
2, aa, bb, cc
Number of rows returned: 6
3, aaa, bbb, ccc
Number of rows returned: 6
Number of rows returned: 6
TEST表内容如下:
(1, 'a', 'b', 'c')
(2, 'aa', 'bb', 'cc')
(3, 'aaa', 'bbb', 'ccc')
输出结果:
1, a, b, c
Number of rows returned: 3
2, aa, bb, cc
Number of rows returned: 3
3, aaa, bbb, ccc
Number of rows returned: 3
Number of rows returned: 3
Number of rows returned: 4
1, a, b, c
Number of rows returned: 5
2, aa, bb, cc
Number of rows returned: 6
3, aaa, bbb, ccc
Number of rows returned: 6
Number of rows returned: 6