Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,什么会导致for循环在值上随机向后移动?_Python_Loops_Sqlite - Fatal编程技术网

在Python中,什么会导致for循环在值上随机向后移动?

在Python中,什么会导致for循环在值上随机向后移动?,python,loops,sqlite,Python,Loops,Sqlite,什么会导致迭代器在循环中向前,然后向后 这是我用来循环表的主键字段的代码。主键用于在单独的表中填充初始值 For循环: for row in sampleDB.select_fromTable(): print row[0] sampleDB.insert_toTable(row[0], 2.0) sqllite插入语句: def insert_toTable(self, primaryKey, initial): c = self.conn.cursor()

什么会导致迭代器在循环中向前,然后向后

这是我用来循环表的主键字段的代码。主键用于在单独的表中填充初始值

For循环:

for row in sampleDB.select_fromTable():
    print row[0]
    sampleDB.insert_toTable(row[0], 2.0)
sqllite插入语句:

def insert_toTable(self, primaryKey, initial):
    c = self.conn.cursor()
    c.execute('insert into insert_toTable VALUES(?, ?)', (primaryKey, initial))
    if c.rowcount == 1:
        print "Updated row.\n"
    else:
        print "Row does not exist.\n"

    self.conn.commit()
    c.close()
创建要循环的元组的select语句:

def select_fromTable(self):
    c = self.conn.cursor()
    c.execute('select * from sampleTable')

    c.close

    return c
下面是一个示例表:

Primary Key     Text
0               John
1               Sue
2               Bill
3               Tyler
在不使用insert语句的情况下运行循环会将每个唯一键打印一次,但如果将调用添加到insert函数(insert_toTable),则会出现以下现象:

0
Updated row.

1
Updated row.

0
Traceback (most recent call last):
sqlite3.IntegrityError: column urlid is not unique
循环的下一次迭代应该是唯一值“2”,而不是返回到“0”


如果需要的话,我可以提供更多的代码部分。

我怀疑这段代码的结果可能会根据使用的sqlite和python的确切版本而变化

请注意,
select\u fromTable
方法中对
c.close
的调用应该是
c.close()
。这是一个幸运的错误,因为在关闭游标后无法迭代查询结果。但是,结果是循环中有两个打开的游标

尝试将您的
选择\u from table
方法更改为:

def select_fromTable(self):
    c = self.conn.cursor()
    c.execute('select * from sampleTable')
    results = c.fetchall()
    c.close()
    return results

看看会发生什么。我相信它会解决您的问题。

我怀疑这段代码的结果可能会根据所使用的sqlite和python的确切版本而变化

请注意,
select\u fromTable
方法中对
c.close
的调用应该是
c.close()
。这是一个幸运的错误,因为在关闭游标后无法迭代查询结果。但是,结果是循环中有两个打开的游标

尝试将您的
选择\u from table
方法更改为:

def select_fromTable(self):
    c = self.conn.cursor()
    c.execute('select * from sampleTable')
    results = c.fetchall()
    c.close()
    return results

看看会发生什么。我相信它会解决你的问题。

在发布这篇文章之后,我试着对commit语句进行注释,并修复了这个奇怪的问题,但为什么?你应该使用语句而不是
INSERT
?据我所知,
commit
只用于事务。我很好奇,为什么这篇文章有
eclipse
标签?@sarnold,SQL UPDATE未应用,因为此表为空,因此没有要更新的行@Elxx,你说得对,提交仅用于更新和其他事务@dappawit,我用eclipse标记它,因为这是我与pydev一起使用的IDE。我很确定是什么导致了这种奇怪的行为,因此标记了eclipse和pydev,以防它们可能是罪魁祸首。发布此消息后,我尝试对commit语句进行注释,并修复了这一奇怪的问题,但为什么?据我所知,您应该使用语句而不是
INSERT
commit
仅用于事务。我很好奇,为什么这篇文章有
eclipse
标记?@sarnold,SQL更新没有应用,因为这个表是空的,因此没有要更新的行@Elxx,你说得对,提交仅用于更新和其他事务@dappawit,我用eclipse标记它,因为这是我与pydev一起使用的IDE。我很确定是什么导致了这种奇怪的行为,因此标记了eclipse和pydev,以防它们可能是罪魁祸首。