TypeError: can't multiply sequence by non-int of type 'float' with sql query
I am trying to multiply sql result with an entry but i have this error :
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'
Here is my Entry code :
self.coef_entry = (Entry(app, width = 10))
self.coef_entry.grid(row=3, column=1, sticky=W)
self.coef_entry = int()
Here is my button code :
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)
Here is my function where i got the error :
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
My Table :
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)\
)"
)
If I run the function without the calculation, the query is working.
So how can I do this calculation with this sql syntax ?
Solution 1:
According to the documentation here the fetchone()
function will either return a Tuple
or None
.
Either way the math operation will not do what you expect. Tuples can't be multiplied by floats and integer multiplication on a Tuple causes a repeat.
I would recommend checking if the row is None, then iterating over each value and doing the multiplication in the loop.
EDIT:
In addition to the Tuple returned from fetchone(), there also may be another issue in the Entry code which stems from the use of the 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
Then after the entry is retrieved from the user EDIT: note this code has been edited to use ‘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
This may not entirely fix your issue since it depends on the order of execution. I would highly recommend reading tutorials on tkinter and how to structure tkinter programs with Python.