【问题标题】:TypeError: unsupported operand type(s) for -: 'int' and 'builtin_function_or_method'TypeError: 不支持的操作数类型 -: 'int' 和 'builtin_function_or_method'
【发布时间】:2018-12-19 21:23:32
【问题描述】:

我试图从我的 sqlite3 数据库中的 product_qty 中减去用户输入的整数,我收到了这个错误:

TypeError: 不支持的操作数类型 -: 'int' 和 'builtin_function_or_method'

这是给出此错误时涉及的两个函数。

def Move():
        curItem = tree.focus()
        print (tree.item(curItem))
        contents = (tree.item(curItem))
        getvalue = contents.get('values')
        print(getvalue)
        selecteditem = contents['values']
        updateStock(id, getvalue[0])
        Database()
        cursor.execute("INSERT INTO `basket` VALUES(?, ?, ?, ?)", getvalue)
        conn.commit()
        PRODUCT_ID.set("")
        PRODUCT_NAME.set("")
        PRODUCT_PRICE.set("")
        PRODUCT_QTY.set("")
        conn.close()

def updateStock(qty, id):
        t = (id,)
        Database()
        cursor.execute("SELECT product_qty FROM product WHERE product_id = ?", t)
        old_qty = cursor.fetchone()
        new_qty = 0
        new_qty = int(old_qty[0]) - qty
        cursor.execute("UPDATE product SET product_qty = ? WHERE product_id = ?", (new_qty, id))
        conn.commit()
        conn.close()

【问题讨论】:

  • 调用updateStock()时的参数顺序与其定义不同。

标签: python python-3.x sqlite tkinter


【解决方案1】:

您将内置的id 函数传递给updateStock。这不会很好地结束。我会避免变量的名称。您需要在 Move 函数中找到一种方法来确定您感兴趣的 ID。

【讨论】:

  • 虽然这有助于我现在知道我必须更改变量的名称,但我将如何确定 ID?
  • updateStock(id, getvalue[0]) 中,将id 替换为您希望在函数调用中出现的qty。 (看起来你的论点倒在那里)
  • @R.Place 您的帖子中没有足够的信息可以说。大概和contents有关。
  • 您是否建议将id 替换为new_qty
  • 不,您的数据库查询似乎需要 ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2014-03-31
  • 2012-12-12
相关资源
最近更新 更多