查找数据库中的数据行
Finding the rows of data in a database
我正在编写 python 脚本,因为我想对数据库中的每 69 行进行计数以获取值列表。我的代码需要一些帮助,因为其中一些是错误的。
当我尝试这个时:
test = con.cursor()
test.execute("SELECT COUNT(*) FROM programs")
x = test.fetchone()[0]
running_total = 1 #Again, you want to start counting at 1 for some reason
while running_total < x:
running_total += 69
print running_total
我会得到这样的结果:
70
139
208
277
346
415
484
553
622
691
760
829
898
967
1036
1105
这是我想要实现的目标:
1
70
139
208
277
346
415
484
553
622
691
760
829
898
967
1036
我想输出以 1
开头的值,然后是 70
、139
、208
...等,直到我得到最后 69 个值一个数据库。示例:我的数据库中有 1104
行,我想要获取的最后 69 行是 1036
并忽略最后一行 1104
。
另一个例子:如果我在数据库中添加另外 69 行数据,我有行 1104
以获得总共 1173
的完整行,我想要获得的最后 69 行是1104
并忽略最后一行 1173
。这将取决于我在数据库中添加了多少行数据。
请问如何输出以1
开头的值,然后每次相加69得到70
、139
、208
...等等,直到我得到最后 69 行然后忽略最后一行?
您应该使用 range/xrange 函数:
range
(Python3) 和 xrange
(Python2) 具有以下形式:range(start, end, increment)
其中 start
包含在内并且 end
是非包容性的。
test = con.cursor()
test.execute("SELECT COUNT(*) FROM programs")
x = test.fetchone()[0]
for running_total in xrange(1, x + 1, 69):
print(running_total)
这应该可以让您获得所需的输出,而无需求助于 C 风格的递增。
我正在编写 python 脚本,因为我想对数据库中的每 69 行进行计数以获取值列表。我的代码需要一些帮助,因为其中一些是错误的。
当我尝试这个时:
test = con.cursor()
test.execute("SELECT COUNT(*) FROM programs")
x = test.fetchone()[0]
running_total = 1 #Again, you want to start counting at 1 for some reason
while running_total < x:
running_total += 69
print running_total
我会得到这样的结果:
70
139
208
277
346
415
484
553
622
691
760
829
898
967
1036
1105
这是我想要实现的目标:
1
70
139
208
277
346
415
484
553
622
691
760
829
898
967
1036
我想输出以 1
开头的值,然后是 70
、139
、208
...等,直到我得到最后 69 个值一个数据库。示例:我的数据库中有 1104
行,我想要获取的最后 69 行是 1036
并忽略最后一行 1104
。
另一个例子:如果我在数据库中添加另外 69 行数据,我有行 1104
以获得总共 1173
的完整行,我想要获得的最后 69 行是1104
并忽略最后一行 1173
。这将取决于我在数据库中添加了多少行数据。
请问如何输出以1
开头的值,然后每次相加69得到70
、139
、208
...等等,直到我得到最后 69 行然后忽略最后一行?
您应该使用 range/xrange 函数:
range
(Python3) 和 xrange
(Python2) 具有以下形式:range(start, end, increment)
其中 start
包含在内并且 end
是非包容性的。
test = con.cursor()
test.execute("SELECT COUNT(*) FROM programs")
x = test.fetchone()[0]
for running_total in xrange(1, x + 1, 69):
print(running_total)
这应该可以让您获得所需的输出,而无需求助于 C 风格的递增。