1

How can I use the python fetchone() call without removing it from the list.

If I do:

while True:
    print cur.fetchone()

where each line is the next row.

How can I do something like

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. 

basically. I need to fetch a row. Check something in it. if it matches, pop if off the list and do stuff to that row. Otherwise. move on.

4

2 回答 2

1

你不能。只需先存储返回值:

while True:
    result = cur.fetchone()
    if result is not None:
        # do something.
于 2013-07-25T13:45:59.410 回答
0

好的。虽然@Martijn Pieters 是正确的并且没有“popless”获取。这是一个解决方法。

        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
于 2013-07-25T19:45:50.820 回答