0

I have a MySQL database with 4 columns: X_coord, Y_coord, num_pos, num_neg

What I want to do is update the num_pos corresponding to a specific location, stored in array called location where location[0] is the X_coord and location[1] is the Y_coord, and I want to do it dynamically like so:

sql = "UPDATE sentimentdata SET num_pos = num_pos + 1 WHERE X_coord = '%s' and Y_coord = '%s'"    %    (location[0],location[1])

My question is will this %s formatting work?

4

1 回答 1

0

是的,它应该工作......你没有尝试过吗?或者你可以这样写

sql = """UPDATE sentimentdata SET num_pos=num_pos+1 WHERE X_coord=%s and Y_coord=%s"""
cursor.execute(sql,(location[0],location[1]))

如果 location[0]=x 和 location[1]=y 它等于:

cursor.execute("""UPDATE sentimentdata SET num_pos=num_pos+1 WHERE X_coord=x and Y_coord=y""")
于 2013-06-20T04:25:28.277 回答