python一行sql太长折成多行并且有多个参数的方法

sql语句

有一个非常长的sql,用编辑器打开编写的时候太长了导致编写非常吃力,而且容易错乱,我想做的是把A,B,C三个变量赋值到sql中的字段中去。

A=1
B=2
C=3
 
sql = "update student t set t.name = '',t.sex = '',t.age = '',t.height = '',t.weight = '',t.class = '',t.stuid = '',t.xxx = '' where t.stuid= '' and t.xxx = '';"


解决方案如下:折叠多行后写法

可以通过()小括号将每一行的字符串整齐拼接,回车以后会自动将每行的字符串进行拼接,并且将每个需要传参的字段加上 %s,在括号结束之前在用%(变量名字,变量名字,变量名字)依次进行赋值。最终完美解决。

A=1
B=2
C=3
sql = ("update student t set t.name = '%s',"
     "t.sex = '%s',"
     "t.age = '%s',"
     "t.height = '%s',"
     "t.weight = '%s',"
     "t.class = '%s',"
     "t.stuid = '%s',"
     "t.xxx = '%s'"
     " where t.stuid= '%s'"
     " and t.xxx = 'P';" %(A,B,A,B,B,A,A,B,C)
  )
发布了105 篇原创文章 · 获赞 17 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_38890412/article/details/104264031