I have a table I want to edit in MySQL using Python, and want to edit only certain rows and skip others.
What I need this for is more complex than in this example, for example a time dependent change in an object the table took data from, but this is the simplest thing I need.
cursor.execute("SELECT * FROM given_table")
result = cursor.fetchall()
for row in result:
whatto = "{Name}".format(Name=row['Action'])
if whatto == "Skip" :
#instruction to print row number
#instruction to skip rest of the loop
#other instructions
How do I tell Python to skip a row and give me the row number (or row number from the selection if I did it with an outset)?
If I write "print(row)", it will print me the row contents, and if I say "print(row[0])", it will print me the contents in column 1.
CodePudding user response:
give me the row number
enumerate
can be used if you need both element and its' position in list or other iterable
How do I tell Python to skip a row
continue
placed inside loop body continues with the next cycle of the nearest enclosing loop.
Using mentioned in your case
for inx, row in enumerate(result):
whatto = "{Name}".format(Name=row['Action'])
if whatto == "Skip" :
print(inx)
continue
#other instructions