网络编程
位置:首页>> 网络编程>> Python编程>> python一行sql太长折成多行并且有多个参数的方法

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

作者:sy_y  发布时间:2022-06-21 03:47:04 

标签: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)
 )

来源:https://blog.csdn.net/s740556472/article/details/77823072

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com