在我的 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 方法是什么?
提前致谢
迈克
请您参考如下方法:
为什么不使用 while 循环?
i=9
while (i<len(headerRow)):
# Attempt to retrieve an entry's corresponding primary key.
row = cursor.fetchone()
if row == None: # New Entry
# Add entry to database
print "New Entry Added!"
else: # Entry already exists
print "Existing Entry"
qpID = row[0]
i += 1
# ...
