Python 根据两列中的值更新mysql数据库
我有一个MySQL数据库,有4列:Python 根据两列中的值更新mysql数据库,python,mysql,mysql-python,Python,Mysql,Mysql Python,我有一个MySQL数据库,有4列:X\u-coord,Y\u-coord,num\u-pos,num\u-neg 我要做的是更新与特定位置对应的num_pos,该位置存储在名为location的数组中,其中location[0]是X_坐标,而location[1]是Y_坐标,我希望这样动态执行: sql = "UPDATE sentimentdata SET num_pos = num_pos + 1 WHERE X_coord = '%s' and Y_coord = '%s'" %
X\u-coord
,Y\u-coord
,num\u-pos
,num\u-neg
我要做的是更新与特定位置对应的num_pos,该位置存储在名为location的数组中,其中location[0]
是X_坐标
,而location[1]
是Y_坐标
,我希望这样动态执行:
sql = "UPDATE sentimentdata SET num_pos = num_pos + 1 WHERE X_coord = '%s' and Y_coord = '%s'" % (location[0],location[1])
我的问题是这个
%s
格式行吗?行,应该行……你没试过吗?
或者你可以这样写
sql = """UPDATE sentimentdata SET num_pos=num_pos+1 WHERE X_coord=%s and Y_coord=%s"""
cursor.execute(sql,(location[0],location[1]))
如果位置[0]=x且位置[1]=y,则等于:
cursor.execute("""UPDATE sentimentdata SET num_pos=num_pos+1 WHERE X_coord=x and Y_coord=y""")
我应该提到,我在mysqldb中使用python