解决 python 对mysql执行操作时报错: pymysql.err.OperationalError: (1054, “Unknown column ‘xxx’ in ‘where clause’”)
我发现我使用mysql往mysql数据库中插入数据的时候,少插入一个字段的数据,然后就想使用update语句把这一个字段的内容插进去:
sql = """UPDATE article set article_content = %s where id = %s""" %(content,article_id)
然后发现报错:
pymysql.err.OperationalError: (1054, "Unknown column '17020ffb957a3738d9faf6de06061354' in 'where clause'")
我就打印了一下我的sql语句,发现是这样的:
UPDATE article set article_content = test where id = 17020ffb957a3738d9faf6de06061354
原本我的想法是我根据函数传参,传过来的就是字符串,不用再加单引号了,但是这样想是错误的,值必须放在单引号里,否则sql可能会把值当做字段
把sql这样写就可以了:
sql = """UPDATE article set article_content = '{}' where id = '{}'""".format(content, article_id)
打印一下发现是正确的sql语句:
UPDATE article set article_content = 'test' where id = '17020ffb957a3738d9faf6de06061354'
更新成功!