【问题标题】:TypeError: can't multiply sequence by non-int of type 'float' with sql queryTypeError:不能用 sql 查询将序列乘以“float”类型的非整数
【发布时间】:2022-01-17 13:19:17
【问题描述】:

我正在尝试将 sql 结果与条目相乘,但出现此错误:

Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "", line 92, in <lambda>
    command=lambda:[self.surface_result(),
  File "", line 189, in surface_result
    row = (cursor.fetchone() * (self.coef_entry / 100))
TypeError: can't multiply sequence by non-int of type 'float'

这是我的入口代码:

self.coef_entry = (Entry(app, width = 10))
self.coef_entry.grid(row=3, column=1, sticky=W)

self.coef_entry = int()

这是我的按钮代码:

research_button = Button(app, text="Rechercher", 
                                    command=lambda:[self.surface_result(), 
                                                    self.eppaisseur_moy_result(), 
                                                    self.eppaisseur_hau_result(),
                                                    self.extrait_sec_moy_result(),
                                                    self.extrait_sec_bas_result(),
                                                    self.coef_melange_result()])
research_button.grid(row=4, column=0, padx=10, pady=10)

这是我得到错误的函数:

def surface_result(self):
        
    cursor.execute("""SELECT helicopter_surface_mouille FROM Helicopter WHERE helicopter_name=%s""",(self.programme_menu_deroulant.get(),))
    row = (cursor.fetchone() * (self.coef_entry / 100))
    for x in row:
        surface_result_label = Label(app, text=x, background = 'white', font=('arial', 12))
        surface_result_label.grid(row=5, column=1, sticky=W, padx=20)
    return row

我的桌子:

mycursor.execute(
    "CREATE TABLE Helicopter(\
        id_produitp INT NOT NULL,\
        id_helicopter INT AUTO_INCREMENT PRIMARY KEY,\
        helicopter_name VARCHAR(20) NOT NULL,\
        helicopter_surface_mouille INT(5) NOT NULL,\
        CONSTRAINT helicopter_ibfk_1 FOREIGN KEY(id_produitp)\
            REFERENCES ProduitP(id_produitp)\
        )"
)

如果我在没有计算的情况下运行该函数,则查询正在运行。

那么我怎样才能用这个 sql 语法来做这个计算呢?

【问题讨论】:

    标签: python mysql tkinter


    【解决方案1】:

    根据文档herefetchone() 函数将返回TupleNone

    无论哪种方式,数学运算都不会达到您的预期。元组不能乘以浮点数,并且元组上的整数乘法会导致重复。

    我建议检查该行是否为 None,然后遍历每个值并在循环中进行乘法运算。

    编辑: 除了 fetchone() 返回的 Tuple 之外,Entry 代码中可能还有另一个问题,它源于使用了tk.Entry

    self.coef_entry = Entry(app, width = 10)
    self.coef_entry.grid(row=3, column=1, sticky=W)
    
    # coef is type tkinter.StringVar
    self.coef = StringVar()
    self.coef_entry['textvariable'] = self.coef
    
    

    然后在从用户那里检索到条目之后 编辑:请注意此代码已被编辑为使用“self.coef.get()”

    def surface_result(self):
            
        cursor.execute("""SELECT helicopter_surface_mouille FROM Helicopter WHERE helicopter_name=%s""",(self.programme_menu_deroulant.get(),))
        row = cursor.fetchone()
        # check if None was returned first 
        if not (row is None):
            for x in row:
                # do the math operation on the individual row values
                t = x * (float(self.coef.get()) / 100)
                surface_result_label = Label(app, text=t, background = 'white', font=('arial', 12))
                surface_result_label.grid(row=5, column=1, sticky=W, padx=20)
        return row
    
    

    这可能无法完全解决您的问题,因为它取决于执行顺序。我强烈建议阅读有关 tkinter 以及如何使用 Python 构建 tkinter 程序的教程。

    【讨论】:

    • 我没有错误了,但是每次都返回 0 就像 self.coef_entry 什么都不返回
    • coef_entry 应该做什么?
    • 看起来你不小心用 self.coef_entry = int() 行破坏了它
    • 用户应该输入一个会影响其他值的系数(coef_entry)
    • 当我删除 self.coef_entry = int() 我有另一个错误:TypeError: unsupported operand type(s) for /: 'Entry' and 'int'
    猜你喜欢
    • 2010-12-30
    • 2012-10-09
    • 1970-01-01
    • 2017-02-04
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多