sqlite&python - 只拉取第一个结果

pen*_*ero 5 python sqlite

这很奇怪(不过,这是我第一次尝试使用python/sqlite),但是如果我执行fetchAll(),我似乎可以获得所有行,但除此之外 - 无论我尝试什么,总是会结束在db中只返回第一行 - 第二次迭代因为返回null而停止.想知道我是如何在python中编写代码的?db好像..

con = sqlite3.connect('backup.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute('select * from tb1;')

for row in cur:
    try:
#       row = dataCur.fetchone()
        #if row == None: break
        print type(row)

        print ' Starting on: %i' % row[0]

        cleaner = Cleaner(scripts=True, remove_tags=['img'], embedded=True)
        try:
            cleaned = cleaner.clean_html(row[2]) #data stored in second col
            cur.execute('update tb1 set data = ? where id = ?;', (cleaned, row[0]))
        except AttributeError:
            print 'Attribute error'

        print ' Ended on: %i' % row[0]

    except IOError:
        print 'IOexception'
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

您正在重用相同的光标来执行两个不同的查询.尝试使用两个不同的游标,看看是否能解决您的问题.