【发布时间】:2011-10-15 06:01:01
【问题描述】:
在我的 python 脚本中,我从索引 9 开始遍历列表 (headerRow)。我想检查它是否已经在数据库中,如果没有,则将其添加到具有自动增强功能的数据库中首要的关键。然后我想再次通过循环发送它以检索它的主键。
for i in range (9, len(headerRow)):
# Attempt to retrieve an entry's corresponding primary key.
row = cursor.fetchone()
print i
if row == None: # New Entry
# Add entry to database
print "New Entry Added!"
i -= 1 # This was done so that it reiterates through and can get the PK next time.
print i
else: # Entry already exists
print "Existing Entry"
qpID = row[0]
# ...
这是我的脚本的输出:
9
New Question Added!
8
10
New Question Added!
9
11
如您所见,我的问题是 range() 不关心 i 的现有值是什么。做我想做的事情的首选 python 方式是什么?
提前致谢,
迈克
【问题讨论】:
标签: python