我想从键和值列表构建一个更新查询,只在必要时在值周围加上引号。现在(使用下面的代码)引号出现在字符串和整数周围。我怎样才能有效地做到这一点?
attributes = ['filename','filesize']
media_id = 12345
sqlbase = """UPDATE media
SET %s
WHERE media_id = %s"""
setpieces = []
values = []
setpieces.append("""timestamp_modified = %s""" % (time.time()))
#Recurse through all attributes in the class
for key in attributes:
#For each key, get the value
if key in attributes:
value = getattr(self, key, None)
setpieces.append("""%s = '%s'""" % (key, value))
query = sqlbase % (', '.join(setpieces), media_id)