如何在不将其从列表中删除的情况下使用python fetchone()调用。
如果我这样做:
while True:
print cur.fetchone()
其中每一行是下一行。
我该怎么做
cur.fetchone(pop=False) # so it doesnt remove it from the list as Im just testing something and I will actually fetch that row later.
基本上。我需要取一行。检查里面的东西。如果它匹配,则弹出如果不在列表中并对该行执行操作。除此以外。继续前进。
答案 0 :(得分:1)
你做不到。只需先存储返回值:
while True:
result = cur.fetchone()
if result is not None:
# do something.
答案 1 :(得分:0)
好。虽然@Martijn Pieters是正确的,但没有“不流行”的提取。这是一个解决方法。
try:
oldwork = 1
work = 1
workingset = []
HasStuff = True
while HasStuff:
result = cur.fetchone()
if result is not None:
work = result[0]
if work == oldwork:
workingset.append(result)
else:
cleanDataSet(workingset)
workingset = []
workingset.append(result)
oldwork = work
else:
cleanDataSet(workingset)
workingset = []
HasStuff = False