1

Alrighty So this is the error

Traceback (most recent call last):
  File "C:/Users/Mandem/PycharmProjects/untitled/Pranks/Lib/site-packages/Ada.py", line 161, in <module>
    mycursor.execute(updaterScoren)
  File "C:\Users\Mandem\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 546, in execute
    stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'

Process finished with exit code 1

This is the command I am trying to run

        f = int(input(
            "Kodeord - bemærk her, at kodeordene er pinkoderne fra min egen database, bare indtast en værdi mellem 1-6"))
        mycursor.execute("SELECT id,adresse,name,pinkode_brugt,score FROM bois WHERE id=%s", (f,))

        # MySQL Commands
        updaterScoren = "UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s", (f,)

I am running it like this:

# MySQL Commands
        updaterScoren = "UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s", (f,)

        myresult = mycursor.fetchall()

        row_count = mycursor.rowcount
        print(myresult)
        print("number of affected rows: {}".format(row_count))
        if row_count == 1:

            print(row_count)
            print(app.timerValue)
            while (i != app.timerValue) and (i <= app.timerValue) and (doorButton != 1) and (app.stopButton != 1):
                print('%.2f' % i)
                i = i + 0.01
                timeLeft = app.timerValue - i
                iTwo = app.timerValue
                print("THIS IS X", app.timerValue)
                time.sleep(0.01)
            # Hvis spillet stoppes, or i ikke når timerens slutværdi, vil scoren gemmes
            if (i != app.timerValue):
                score = i
                print("Dette er dit resultat: {}".format('%.2f' %score))
                mycursor.execute(updaterScoren)

Sidenote, is it possible to also use "myresult" somehow to identify which row to change/update? So that, something ala "UPDATE pinkode_brugt WHERE row = Myresult"

Here is my full code:

import threading
import tkinter as tk
import pip
import time
import mysql.connector
from time import sleep




# NY THREAD(s), der kan vise scoreboarden på en skærm.
class Timer(threading.Thread):
    def _init_(self):
        threading.Thread._init_(self)
        self.label = 0
        global score


    def run(self) -> None:
        self.master = tk.Tk()

        self.master.gemoetry("+0+0")

        self.master = tk.Label(self.root, bg = "black", fg = "red")

        self.timer.grid(row = 1, column = 2)

        self.master.mainloop()


class Tkinter(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.daemon = True
        self.root = 0
        self.entry = 0
        self.label = 0
        self.button = 0
        self.timerValue = 0
        self.stopButton = 0

        self.start()

    def run(self) -> 0:
        self.root = tk.Tk()

        # tkinter code goes here
        self.root.geometry("+2+0")
        self.root.config(background="black")

        self.label = tk.Label(self.root, bg="white", fg="black", font=("Fixedsys", 28))
        self.entry = tk.Entry(self.root, bg="white", fg="black", font=("Fixedsys", 28))
        self.stopTiden = tk.Button(self.root, bg="red", fg="white", text="Stop Spillet", command=self.stopTid)
        self.startTiden = tk.Button(self.root, bg="green", fg="white", text="Start Spillet", command=self.startTid)

        self.timerDisplay = tk.Label(self.root, bg="black", fg="red", text="Ceci n'est pas une timer")

        self.timerDisplay.grid(row=2, column=50)
        self.startTiden.grid(row=0, column=1)
        self.stopTiden.grid(row=0, column=10)
        self.entry.grid(row=1, column=1)
        self.label.grid(row=1, column=10)

        self.root.bind("<KeyPress>", self.read)
        self.root.mainloop()

    def displayTime(self):
        self.timerDisplay.config(text=i)

    def read(self, event):
        key = event.keysym
        try:
            if key == "Return":
                self.getA()
                self.visTid()
        except:
            self.fejlTidvalg()

    def startTid(self):
        self.stopButton = 0

    def getA(self):
        self.timerValue = float(self.entry.get().format("%.2f", 1.23456))
        print(self.timerValue)
        return self.timerValue

    def stopTid(self):
        self.stopButton = 1

    def visTid(self):
        self.label.config(text="Det her er timerens slutværdi: {}".format(self.timerValue))

    def fejlTidvalg(self):
        self.label.config(text="FEJL -Vælg Venligst Et Tal")


app = Tkinter()

stopTiden = 0

# Login til databasen
mydb = mysql.connector.connect(
    host="****",
    user="****",
    password="*****",
    database='mydatabase',
    auth_plugin='mysql_native_password'
)

doorButton = 0

while (doorButton != 1):
    stopButton = app.stopButton
    timerValue = app.timerValue
    iTwo = timerValue
    doorButton = 0
    i = 0.00
    mycursor = mydb.cursor()


    def countdown(i):

        while i:
            mins, secs = divmod(t, 60)
            timer = '{:02d}:{:02d}'.format(mins, secs)
            print(timer, end="\r")
            time.sleep(1)
            t -= 1


    # Servodøren skal åbnes her i denne kode
    while (app.stopButton != 1):
        # Indtast Pinkoden
        f = int(input(
            "Kodeord - bemærk her, at kodeordene er pinkoderne fra min egen database, bare indtast en værdi mellem 1-6"))
        mycursor.execute("SELECT id,adresse,name,pinkode_brugt,score FROM bois WHERE id=%s", (f,))

        # MySQL Commands
        updaterScoren = "UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s", (f,)

        myresult = mycursor.fetchall()

        row_count = mycursor.rowcount
        print(myresult)
        print("number of affected rows: {}".format(row_count))
        if row_count == 1:

            print(row_count)
            print(app.timerValue)
            while (i != app.timerValue) and (i <= app.timerValue) and (doorButton != 1) and (app.stopButton != 1):
                print('%.2f' % i)
                i = i + 0.01
                timeLeft = app.timerValue - i
                iTwo = app.timerValue
                print("THIS IS X", app.timerValue)
                time.sleep(0.01)
            # Hvis spillet stoppes, or i ikke når timerens slutværdi, vil scoren gemmes
            if (i != app.timerValue):
                score = i
                print("Dette er dit resultat: {}".format('%.2f' %score))
                mycursor.execute(updaterScoren)


            else:
                print("Ingen point")
                mycursor.execute()


        i = 0
    # Her skal du tilføje i til den ID's row, der lige har åbnet via sin pinkode. Så er du stort set færdig! :D

Ps. I have looked at the other posts regarding the same issue. As far as I understood it, their solutions didn't help.

5
  • Can you try printing the variable operation? you are treating it like an string but somewhere it is being returned as a tuple Commented Oct 26, 2020 at 20:52
  • I did "print(updaterScoren)", and it returned this ('UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s', (3,)) Commented Oct 26, 2020 at 20:55
  • this comnd runs in python3.86 but you must check f that doesn't look right Commented Oct 26, 2020 at 21:10
  • @nbk Which command runs in python 3.86? Mine? If yes, what are the implications of that? Also, it was wrong, I changed it, and it worked syntaxically, but semantically it still doesn't incriment the value or replace "mysql"-score, with my variable score (score = i). Commented Oct 26, 2020 at 21:55
  • yes, i don't see in your code where you check if the select that works has an answer on that select. so i would check if it gets an reply that is not null. Commented Oct 26, 2020 at 22:06

1 Answer 1

1

Changing updaterScoren = "UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s", (f,) to updaterScoren = "UPDATE bois SET pinkode_brugt = pinkode_brugt + 1 WHERE id = %s" % f
should do the job as you are using format variables incorrectly

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help, Himanshu. Your solution did indeed fix my syntax error. Problem is now, I have a semantic error. The value "pinkode_brugt" doesn't update/increment. Do you have any idea, why this is? Also, could you please explain, what the change in the code did, so I can avoid this mistake in the future?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.