In my python script, I am itterating through a list (headerRow) starting at index 9. I want to check to see if it is already in the database, and if not then add it to a database with an auto-incementing primary key. I then want to send it through the loop again to retrieve it’s primary key.
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]
# ...
Here is the output of my script:
9
New Question Added!
8
10
New Question Added!
9
11
As you can see, my issue is that range() doesn’t care what the existing value of i is. What is the preferred python way to do what I’m trying to do?
Thanks in advance,
Mike
Why not use a
whileloop?