PYTHON:使用python变量更新MULTIPLE COLUMNS
发布时间:2020-12-20 11:11:13 所属栏目:Python 来源:网络整理
导读:我正在尝试编写一个有效的 mysql语句,它允许我使用作为 python变量提供的值更新一个记录中的多个列. 我的陈述如下: db = MySQLdb.connect(host="localhost",user="user",passwd="password",db="dbname")cursor = db.cursor()sql_update = "UPDATE table_nam
我正在尝试编写一个有效的
mysql语句,它允许我使用作为
python变量提供的值更新一个记录中的多个列.
我的陈述如下: db = MySQLdb.connect(host="localhost",user="user",passwd="password",db="dbname") cursor = db.cursor() sql_update = "UPDATE table_name SET field1=%s,field2=%s,field3=%s,field4=%s,field5=%s,field6=%s,field7=%s,field8=%s,field9=%s,field10=%s WHERE id=%s" % (var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,id) cursor.execute(sql_update) cursor.close () db.commit() db.close() 在尝试执行查询时,我不断收到SQL语法中存在错误的信息.我找不到它. 解决方法
您正在使用字符串格式,而您应该做的是使用参数化查询.像这样做:
cursor.execute("UPDATE table_name SET field1=%s ... field10=%s WHERE id=%s",(var1,... var10,id)) 你真的需要发布10个变量吗?格式化令人沮丧,我放弃了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |