类型错误:Python“方法”无法转换为 MySQL 类型

问题描述

好的,让我解释一下发生了什么我正在使用 tkinter 表单并正在填写正在填写的信息,我正在从包含电影的数据库中选择信息。我已经写了两次这段代码,一次是在没有从 tkinter 表单中获取信息的情况下编写的,然后第二次编写了从 tkinter 表单中获取的信息。我不确定错误来自哪里,因为代码没有改变。
这是运行正常的代码

def PersonalityQuiz():
QuizUI = tk.Tk()
QuizUI.title("Personality Quiz")
QuizUI.geometry('700x300')

firstname = input("What is your first name?")
surname = input("What is your surname?")

answer1 = q1()
answer2 = q2()
answer3 = q3()
answer4 = q4()
answer5 = q5()

print(answer4,answer5)

sql = "SELECT * FROM Movies WHERE PG_rating <= %s AND Minutes <= %s AND Year >= %s AND Genre = %s AND Sub_Genre = %s ORDER BY imdb desc"
statement = (answer1,answer2,answer3,answer4,answer5)

sqlFilmID = "SELECT FilmID FROM Movies WHERE PG_rating <= %s AND Minutes <= %s AND Year >= %s AND Genre = %s AND Sub_Genre = %s"

mycursor = mydb.cursor()
mycursor.execute(sql,statement)
myresult = mycursor.fetchall()

listBox = tk.ListBox(QuizUI)
listBox.pack()

for i in myresult:
    listBox.insert(tk.END,i)

mycursor.execute(sqlFilmID,statement)
FilmID = mycursor.fetchall()

sqlinsert = "INSERT INTO users (FirstName,Surname) VALUES (%s,%s)"
vals = (firstname,surname)
mycursor.execute(sqlinsert,vals)
mydb.commit()

UserID = mycursor.lastrowid

for i in FilmID:
    LinkInsert = "INSERT INTO userlink (UserID,FilmID) VALUES (%s,%s)"
    vals = (str(UserID),str(i[0]))
    mycursor.execute(LinkInsert,vals)
    mydb.commit()

QuizUI.mainloop()

错误代码如下:

def guiform():
genres = [
    "Action","Adventure","Animation","Comedy","Crime","Documentry","Drama","Family","Fantasy","Horror","Musical","Sci-Fi","Spy","Superhero","War",] #etc

guitest = tk.Tk()
guitest.geometry("700x400")

frame1 = tk.Frame(master=guitest,height=200,width=100,bg="crimson")
frame1.pack(fill=tk.BOTH,side=tk.LEFT,expand=True)

values = tk.StringVar(guitest)
values.set(genres[0]) # default value

lblfirstname = tk.Label(frame1,text="First Name")
lblfirstname.grid(row=1,column=2)
ENTfirstname = tk.Entry(frame1)
ENTfirstname.grid(row=2,column=2)

lblsurname = tk.Label(frame1,text="Surname")
lblsurname.grid(row=3,column=2)
ENTsurname = tk.Entry(frame1)
ENTsurname.grid(row=4,column=2)

lbl1 = tk.Label(frame1,text="How old are you?")
lbl1.grid(row=1,column=0)
question1 = tk.Entry(frame1)
question1.grid(row=2,column=0)

lbl2 = tk.Label(frame1,text="What is the longest you would want a film to be? (minutes)")
lbl2.grid(row=4,column=0)
question2 = tk.Entry(frame1)
question2.grid(row=5,column=0)

lbl3 = tk.Label(frame1,text="What is the oldest year you would watch a movie from (e.g. 1985)")
lbl3.grid(row=6,column=0)
question3 = tk.Entry(frame1)
question3.grid(row=7,column=0)

lbl4 = tk.Label(frame1,text="What is your preferred Genre?")
lbl4.grid(row=8,column=0)
w = tk.OptionMenu(frame1,values,*genres)
w.grid(row=9,column=0)

submit = tk.Button(frame1,text="Submit information",command=lambda:infoget(question1.get,question2.get(),question3.get(),values.get()),width=18)
submit.grid(column=0,row=10)

listBox=tk.ListBox(frame1)
listBox.grid(row=0,column=1,rowspan=10)

def infoget(answer1,answer4):
    sql = "SELECT * FROM Movies WHERE PG_rating <= %s AND Minutes <= %s AND Year >= %s AND Genre = %s ORDER BY imdb desc"
    statement = (answer1,answer4)

    mycursor = mydb.cursor()
    mycursor.execute(sql,statement)
    myresult = mycursor.fetchall()

    for i in myresult:
        listBox.insert(tk.END,i)

    sqlFilmID = "SELECT FilmID FROM Movies WHERE PG_rating <= %s AND Minutes <= %s AND Year >= %s AND Genre = %s ORDER BY imdb desc"
    mycursor.execute(sqlFilmID,statement)
    FilmID = mycursor.fetchall()

    
    sqlinsert = "INSERT INTO users (FirstName,%s)"
    vals = (str(firstname),str(surname))
    mycursor.execute(sqlinsert,vals)
    mydb.commit()

    #Returns the Primary Key of the last row accessed by the sql cursor
    UserID = mycursor.lastrowid

    for i in FilmID:
        LinkInsert = "INSERT INTO userlink (UserID,%s)"
        vals = (str(UserID),str(i[0]))
        mycursor.execute(LinkInsert,vals)
        mydb.commit()

    guitest.update

guitest.mainloop()

这是它产生的可爱错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\conversion.py",line 183,in to_MysqL
    return getattr(self,"_{0}_to_MysqL".format(type_name))(value)
AttributeError: 'MysqLConverter' object has no attribute '_method_to_MysqL'

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\cursor.py",line 432,in _process_params
    res = [to_MysqL(i) for i in res]
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\cursor.py",in <listcomp>
    res = [to_MysqL(i) for i in res]
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\conversion.py",line 185,in to_MysqL
    raise TypeError("Python '{0}' cannot be converted to a "
TypeError: Python 'method' cannot be converted to a MysqL type

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\tkinter\__init__.py",line 1884,in __call__
    return self.func(*args)
  File "Z:\My Files\Student My Documents\A Level\Computing\NEA\nea2.py",line 168,in <lambda>
    submit = tk.Button(frame1,width=18)
  File "Z:\My Files\Student My Documents\A Level\Computing\NEA\nea2.py",line 179,in infoget
    mycursor.execute(sql,statement)
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\cursor.py",line 557,in execute
    psub = _ParamSubstitutor(self._process_params(params))
  File "C:\Users\lupin\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\cursor.py",line 436,in _process_params
    raise errors.ProgrammingError(
MysqL.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'method' cannot be converted to a MysqL type

我不知道是什么造成和/或导致了这个错误。如果有人可以帮助解决此问题或为我指明解决此问题的正确方向,我将不胜感激!
谢谢你,卢平

解决方法

传递的第一个参数是下面一行中的一个函数:

command=lambda:infoget(question1.get,question2.get(),question3.get(),values.get())

应该是:

command=lambda:infoget(question1.get(),values.get())

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...