python SQL查询:一元+的错误操作数类型:'str'

时间:2020-10-30 10:38:25

标签: python sql pyodbc

我在python中多次遇到这种类型的问题。而且我只想创建一个sql更新查询。

我的脚本如下所示

update1 = '''UPDATE User_Answer_Knowledge SET'''
+ "integration =" + str(21) + ", scope =" + str(12) + ", schedule =" + str(13) + ", cost =" + str(14) 
+ ", quality =" + str(15) + ", resource =" + str(16) + ", communication =" + str(17) + "WHERE User_Key = 1"

那到底是什么问题?我也尝试了以下代码,但是出现了同样的问题

update1 = '''UPDATE User_Answer_Knowledge SET'''
+ "integration =" + 21 + ", scope =" + 12 + ", schedule =" + 13 + ", cost =" + 14 
+ ", quality =" + 15 + ", resource =" + 16 + ", communication =" + 17 + "WHERE User_Key = 1"
update1 = '''UPDATE User_Answer_Knowledge SET'''
+ "integration =" + "21" + ", scope =" + "12" + ", schedule =" + "13" + ", cost =" + "14" 
+ ", quality =" + "15" + ", resource =" + "16" + ", communication =" + "17" + "WHERE User_Key = 1"
update1 = '''UPDATE User_Answer_Knowledge SET'''
+ "integration =" + '21' + ", scope =" + '12' + ", schedule =" + '13' + ", cost =" + '14' \+ ", quality =" + '15' + ", resource =" + '16' + ", communication =" + '17' + "WHERE User_Key = 1"

3 个答案:

答案 0 :(得分:0)

您需要在行尾添加一些\

update1 = '''UPDATE User_Answer_Knowledge SET''' \
+ "integration =" + str(21) + ", scope =" + str(12) + ", schedule =" + str(13) + ", cost =" + str(14) \
+ ", quality =" + str(15) + ", resource =" + str(16) + ", communication =" + str(17) + "WHERE User_Key = 1"

否则,字符串声明以新行结尾

答案 1 :(得分:0)

您似乎已将语句分成多行。因此,它抱怨以+符号开头的行。

有多种方法可以解决此问题。您可以:

  • 将所有串联写在一行上
  • 在行尾使用\
  • 在所有参数周围使用括号

enter image description here

答案 2 :(得分:0)

@rdas已经回答了,但我也想强调一下您的查询容易受到sql injection的攻击 您应该像这样对输入进行参数设置

"update table value = {0},value2={1}".format('21','99')
>>> 'update table value = 21,value2=99'