skip to Main Content

I would like to ask an question like this It makes me Struggle. Please Help

How Can I Fix This error While Updating the Table

Here’s The code

Heres The Update Code

def update():
        databases = mysql.connector.connect(
        host ="localhost",
        user = "userdata",
        password = "",
        database = "facerecog"
        )
        conn = databases.cursor()
        conn.execute("UPDATE record set names=%s, course_year=%s, position=%s"(t4.get(),t5.get(),t6.get()))
        databases.commit
        databases.close
        messagebox.showinfo("NOTICE", "UPDATED SUCCESSFULLY")

And here The data show Selected Shows on my text box

    def showtable(tree):
        viewinfo = tree.focus()
        data = tree.item(viewinfo)
        row = data['values']
        t4.set(row[0])
        t5.set(row[1])
        t6.set(row[2])

And Here’s The button

    updatebutton = tk.Button(t, text = "Update Data", command =update)
    updatebutton.configure(font = ('Cooper', 13, 'bold'), bg = 'sky blue', fg = 'black')
    updatebutton.place(x=560, y=360)
    t.bind("<ButtonRelease-1>",showtable)

And then It Appears error Like this

py:347: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
  conn.execute("UPDATE record set names=%s, course_year=%s, position=%s"(t4.get(),t5.get(),t6.get()))

3

Answers


  1. I think the code should be a string formatting, it is missing %:

    conn.execute("UPDATE record set names=%s, course_year=%s, position=%s" % (t4.get(),t5.get(),t6.get()))
    
    Login or Signup to reply.
  2. Modulo string formatting is kind of ancient, I prefer f-strings:

    conn.execute(f"UPDATE record set names={t4.get()}, course_year={t5.get()}, position={t6.get()}")
    
    Login or Signup to reply.
  3. The error is telling you what to fix: it seems like you have invalid format.

    The docs that I was able to find for the mysql.connector seems to confirm that you would separate formatted arguments with a comma , as I had initially suspected. By the way, the log methods in the python logging module work the same way as well, which is pretty handy.

    So in your case, I’d replace that line:

    conn.execute(update_stmt(t4.get(),t5.get(),t6.get()))
    

    where update_stmt is defined as

    "UPDATE record set names=%s, course_year=%s, position=%s"
    

    with the following line:

    conn.execute(update_stmt, (t4.get(),t5.get(),t6.get()))
    

    As an aside, you’ll have noticed that the error you got is actually very helpful in this case:

    py:347: SyntaxWarning: 'str' object is not callable; perhaps you missed a comma?
    

    Which is actually the very thing we needed to do to resolve the error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search