我查询两个数据库以获得两个关系。我对这些关系进行过一次形成地图的演练,然后再进行一些计算。但是,当我第二次尝试迭代相同的关系时,我发现实际上没有发生迭代。下面是代码:多次对表进行迭代Python SQLAlchemy
dev_connect = dev_engine.connect()
prod_connect = prod_engine.connect() # from a different database
Relation1 = dev_engine.execute(sqlquery1)
Relation2 = prod_engine.execute(sqlquery)
before_map = {}
after_map = {}
for row in Relation1:
before_map[row['instrument_id']] = row
for row2 in Relation2:
after_map[row2['instrument_id']] = row2
update_count = insert_count = delete_count = 0
change_list = []
count =0
for prod_row in Relation2:
count += 1
result = list(prod_row)
...
change_list.append(result)
count2 = 0
for before_row in Relation1:
count2 += 1
result = before_row
...
print count, count2 # prints 0
before_map
和after_map
不是空的,所以Relation1
和Relation2
肯定有他们的元组。然而,count
和count2
为0,所以prod_row
和before_row
'for循环'实际上并未发生。为什么我不能第二次迭代Relation1
和Relation2
?
有没有办法重新打开游标? –
@JeremyFisher - 据我所知,SQL Alchemy/DBAPI不支持[可滚动游标](https://en.wikipedia.org/wiki/Cursor_%28databases%29#Scrollable_cursors),所以你的光标回来是一次使用而忘记的交易。 – birryree