python - 错误 : not all arguments converted during bytes formatting

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

最佳答案

您正在使用“?” ("qmark") 样式用于参数替换字符,但 MySQLdb 需要 "%s"("format") 样式。

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 上更具可读性 - 您无需在自己的代码中执行此操作)。

关于python - 错误 : not all arguments converted during bytes formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55198217/

相关文章:

python - 如何用python3.6安装theano?

python - Keras 中的 "cell class"是什么?

python - 未在 featuretools 中为我的实体集设置生成功能

python - 简单的Python问题: Why can't I assign a variable to a sorted list (in place)?

php - 尝试了所有仍然出现 ERROR 2002 (HY000) : Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in mac 的事情

php - 从数据库中取出一条记录到选择列表

php - 如何为 "mac"设置读取文件权限,类似于 PC 的右键单击

python - 我可以让 mean_iou 依赖于 update_op 吗?它们都由 tf.metrics.mean_iou() 返回

python - 如何使用 bool 掩码在 pandas DataFrame 中用 nan 替换 'any strings'?

django - 为什么函数可以返回函数本身