当前位置:主页 > 软件编程 > Python代码 >

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

时间:2021-08-31 09:59:42 | 栏目:Python代码 | 点击:

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)
  )

您可能感兴趣的文章:

相关文章