我在使用where子句中的键从python字典创建MySQL更新语句时遇到一些挑战
这是我苦苦挣扎的代码的样子:
for key, values in d.items():
sql = """UPDATE news SET title={},avail={},URL={},status={},degree={}""".format(key, .join(values[0::2]),.join(values[1::3]),.join(values[2::4]),.join(values[3::5]))
print(sql)
这是我字典中的值
d={'ZB06WW34556GND5G': [u'[<font color="red">Google</font>] News: Sports & Outdoors',
' Only 13',
u'https://www.google.com',
u'13',
'-2.0'],
'ZB06WWG4444444ND5G': [u'[<font color="red">CNN</font>] News: Sports & Outdoors',
' Only 15',
u'https://www.cnn.com',
u'14',
'-4.0']
}
以下是预期结果:
UDPATE new SET title="[<font color="red">Google</font>] News: Sports & Outdoors",avail="Only 13",URL="https://www.google.com",status="13",degree="-2.0" where id="ZB06WW34556GND5G"
UDPATE new SET title="[<font color="red">CNNGoogle</font>] News: Sports & Outdoors",avail="Only 15",URL="https://www.cnn.com",status="14",degree="-4.0" where id="ZB06WWG4444444ND5G"
有人可以指导我正确的方向吗?我迷失了关键和价值观...
最佳答案
以下代码给出了预期的输出:
for key, values in d.items():
sql = """UPDATE news SET title = '{}', avail = '{}', URL = '{}', status = '{}', degree = '{}' where id = '{}'""".format(d[key][0], d[key][1], d[key][2], d[key][3], d[key][4], key)
print(sql)
关于mysql - 从Python字典中使用where子句中的键创建mysql更新语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50145152/