mysql - python嵌套SQL语句,字段 in () 格式错误?
伊谢尔伦
伊谢尔伦 2017-04-18 10:17:43
0
1
337

记录下,防止以后忘掉

原因是在mysql 中, 字段 in 后边应该加元祖(),而现在代码中传进去的参数是个列表,造成语法错误
改正:

将要传递的参数使用 join()生成一个字符串传进去,另外,在MySQL语句中,dtu_id in (%s)(格式符放进元祖括号中)


新手上路,欢迎老司机指导纠正

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
黄舟

Personal writing method for reference

db = MySQLdb.connect(*****)
cursor = db.cursor()
sql = "update test set name = 'test' where id in(%s)"
args = [1, 2, 3]
in_p = ', '.join((map(lambda x: '%s', args)))
cursor.execute(sql % in_p, args)
db.commit()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!