【问题标题】:Python read data from mysql errorsPython从mysql错误中读取数据
【发布时间】:2018-06-09 17:56:09
【问题描述】:

我想从mysql读取一列,乘以一个因子并写入mysql

cursor = db.cursor()
query = ('SELECT GDP FROM selectedairport')
cursor.execute(query)
gdp_list = []
for GDP in cursor:
    gdp_list.append(GDP)

weight = 0.5
num = gdp_list * weight
cursor.execute("UPDATE `selectedairport` SET `selectedairport`.`GDP`=num")

但是,这会产生错误:

TypeError: can't multiply sequence by non-int of type 'float'

我找到了 gdp_list

[(50,),
 (60,),
 (80,),
 (40,)]

可能是什么问题?

【问题讨论】:

    标签: python-3.x mysql5


    【解决方案1】:

    如果您想通过将数字列表乘以一个因子来创建一个列表,则必须将每个数字单独相乘,例如:

    num = [g * weight for g in gdp_list]
    

    但坦率地说,没有理由这样做,甚至没有理由先查询表。您可以直接使用update 语句将列乘以一个因子:

    cursor.execute("UPDATE `selectedairport` SET `selectedairport`.`GDP`= `GDP` * 0.5)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多