在用pymysql操作数据库的过程中,给sql语句传参碰到了很多问题,网上传参策略很多,这里推荐两种
单个传参用%s,写起来比较简单:
field = '-'
sql_talk="UPDATE cnp.Test set a='' where b='%s'" cursor.execute(sql_talk % field) db.commit()
多个传参用{0}占位符:
field = '-'
a = 'code'
sql_talk="UPDATE cnp.Test set {0}='' where business_registration_code='{1}'".format(a,field) cursor.execute(sql_talk) db.commit()