在我的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
正如你所看到的,我的問題是,範圍()不關心什麼i
現在值。什麼是我想要做什麼的首選python方法?
由於提前,
邁克
它的工作原理很簡單。我喜歡。現在更好的問題是爲什麼使用for循環在我的腦海中如此根深蒂固。謝謝。 – MikeKusold