错误:在字节格式化期间并非所有参数都已转换

时间:2019-03-16 15:07:58

标签: 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=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

2 个答案:

答案 0 :(得分:0)

您使用的是“?” (“ qmark”)样式用于参数替换字符,但MySQLdb希望使用“%s”(“格式”)样式。

Python数据库驱动程序模块可以使用多种不同的方式来格式化变量以包含在SQL语句中。可以通过检查模块的paramstyle属性来确定特定模块接受的样式:

>>> import MySQLdb
>>> MySQLdb.paramstyle
'format'

可能的格式的完整列表为here

您需要将INSERT语句的VALUE子句中的每个问号替换为“%s”:

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))

(我将语句分成两个字符串,以使其在Stack Overflow上更具可读性-您无需在自己的代码中执行此操作)。

答案 1 :(得分:-1)

首先,我主张一种面向对象的编程风格,

conn = mc.connect(host =“ localhost”,user =“ root”,password =“ ashu12”,database =“ python”)

conn = mc.connect(host =“ localhost”,user =“ root”,passwd =“ ashu12”,db =“ python”)