首页 > 解决方案 > python SQL查询:一元+的错误操作数类型:'str'

问题描述

我在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"

标签: pythonsqlpyodbc

解决方案


你需要一些\在行尾:

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"

否则字符串声明以新行结束


推荐阅读