Python 错误:在字节格式化过程中未转换所有参数
我只是想用PYTHON制作一个GUI应用程序。我是一个初学者,我被这个错误困住了 节目:Python 错误:在字节格式化过程中未转换所有参数,python,mysql,python-3.x,Python,Mysql,Python 3.x,我只是想用PYTHON制作一个GUI应用程序。我是一个初学者,我被这个错误困住了 节目: from tkinter import * import MySQLdb as mc root = Tk() root.geometry('500x500') root.title("Open Source : Python Project by Ashutosh Joshi") root.config(bg="#150015") First_Name=StringVar() Last_Name=St
from tkinter import *
import MySQLdb as mc
root = Tk()
root.geometry('500x500')
root.title("Open Source : Python Project by Ashutosh Joshi")
root.config(bg="#150015")
First_Name=StringVar()
Last_Name=StringVar()
Roll_No = IntVar()
Phone_No=IntVar()
Email_ID=StringVar()
def database():
fname=First_Name.get()
lname=Last_Name.get()
rollno=Roll_No.get()
phonenumber=Phone_No.get()
emailid=Email_ID.get()
print(type(phonenumber))
conn = mc.connect(host="localhost", user="root", password="ashu12",database="python")
cursor=conn.cursor()
cursor.execute('INSERT INTO studentdetails (First_Name,Last_Name,Roll_No,Phone_No,Email_ID) VALUES(?,?,?,?,?)',(fname,lname,rollno,phonenumber,emailid))
conn.commit()
label_0 = Label(root, text="STUDENT DATABASE",width=20,font=("bold", 25) ,fg="White",bg="#150015")
label_0.place(x=60,y=53)
label_1 = Label(root, text="First Name",width=20,font=("bold", 10),bg="Yellow",fg="Black")
label_1.place(x=80,y=130)
entry_1 = Entry(root,textvar=First_Name)
entry_1.place(x=250,y=130)
label_2 = Label(root, text="Last Name",width=20,font=("bold", 10),bg="Yellow",fg="Black")
label_2.place(x=80,y=180)
entry_2 = Entry(root,textvar=Last_Name)
entry_2.place(x=250,y=180)
label_3=Label(root,text="Roll No",width=20,font=("bold",10),bg="Yellow",fg="Black")
label_3.place(x=80,y=230)
entry_3 = Entry(root,textvar=Roll_No)
entry_3.place(x=250,y=230)
label_4=Label(root,text="Phone No",width=20,font=
("bold",10),bg="Yellow",fg="Black")
label_4.place(x=80,y=280)
entry_4 = Entry(root,textvar=Phone_No)
entry_4.place(x=250,y=280)
label_4=Label(root,text="Email ID",width=20,font=("bold",10),bg="Yellow",fg="Black")
label_4.place(x=80,y=330)
entry_4 = Entry(root,textvar=Email_ID)
entry_4.place(x=250,y=330)
Button(root, text='Submit',width=20,bg='brown',fg='white',command=database).place(x=180,y=380)
root.mainloop()
但我遇到了这个错误
<class 'int'>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\python37\lib\site-packages\MySQLdb\cursors.py", line 201, in execute
query = query % args
TypeError: not all arguments converted during bytes formatting
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:\osproject.py", line 26, in database
cursor.execute('INSERT INTO studentdetails (First_Name,Last_Name,Roll_No,Phone_No,Email_ID) VALUES(?,?,?,?,?)',(fname,lname,rollno,phonenumber,emailid))
File "C:\python37\lib\site-packages\MySQLdb\cursors.py", line 203, in execute
raise ProgrammingError(str(m))
MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting
Tkinter回调中的异常
回溯(最近一次呼叫最后一次):
文件“C:\python37\lib\site packages\MySQLdb\cursors.py”,第201行,在execute中
query=查询%args
TypeError:在字节格式化过程中并非所有参数都已转换
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\python37\lib\tkinter\\ uuuuu init\uuuuu.py”,第1705行,在调用中__
返回self.func(*args)
数据库中第26行的文件“D:\osproject.py”
cursor.execute('INSERT INTO studentdetails(名、姓、卷号、电话号、电子邮件号)值(?、、?、?),(fname、lname、卷号、电话号码、电子邮件号))
文件“C:\python37\lib\site packages\MySQLdb\cursors.py”,第203行,在execute中
raise编程错误(str(m))
MySQLdb.\u exceptions.ProgrammingError:在字节格式化过程中,并非所有参数都已转换
I
首先,我提倡一种面向对象的编程风格,不管怎样
conn=mc.connect(host=“localhost”,user=“root”,password=“ashu12”,database=“python”)
它是
conn=mc.connect(host=“localhost”,user=“root”,passwd=“ashu12”,db=“python”)您正在使用“?”(“qmark”)样式作为参数替换字符,但MySQLdb需要“%s”(“格式”)样式
Python数据库驱动程序模块可以使用多种不同的方式格式化变量,以便包含在SQL语句中。特定模块接受的样式可以通过检查模块的paramstyle
属性来确定:
>>> import MySQLdb
>>> MySQLdb.paramstyle
'format'
可能格式的完整列表如下所示
您需要将INSERT语句的VALUE子句中的每个问号替换为“%s”:
(我已将语句分成两个字符串,以使其在堆栈溢出时更具可读性-您不需要在自己的代码中执行此操作)。idk但仍然存在未知错误…..感谢您的回答我怀疑如果不编辑MySQLdb库的源代码,该库定义了
connect()
,此操作是否有效。通过这样做,他将为其他人破解密码。
cursor.execute(
"""INSERT INTO studentdetails (First_Name,Last_Name,Roll_No,Phone_No,Email_ID) """
"""VALUES(%s,%s,%s,%s,%s)""",
(fname,lname,rollno,phonenumber,emailid))