如何解决insert缺少1个必需的位置参数:'string'使用python mysql时出现相同的错误
我是python的初学者。搜索数据如何使用python MysqL传递到相关的文本框。到目前为止,我尝试了什么,我在下面附加了代码。搜索数据显示成功,我在控制台上看到了数据。如何传递到我不知道的相关文本框中 错误 insert()缺少1个必需的位置参数:“字符串”相同的错误
from tkinter import *
from tkinter import messageBox
import MysqL.connector
def Ok():
global myresult
studname = e1.get()
coursename = e2.get()
MysqLdb=MysqL.connector.connect(host="localhost",user="root",password="",database="smschool")
mycursor=MysqLdb.cursor()
try:
mycursor.execute("SELECT * FROM record where id = ?",(studname,))
myresult = mycursor.fetchall()
for x in myresult:
print(x)
e2.insert(str(x[2]))
e3.insert(str(x[3]))
except Exception as e:
print(e)
MysqLdb.rollback()
MysqLdb.close()
root = Tk()
root.title("Search MysqL")
root.geometry("300x200")
Label(root,text="Student ID").place(x=10,y=10)
Button(root,text="Search",command=Ok,height = 1,width = 13).place(x=140,y=40)
Label(root,text="Course").place(x=10,y=80)
Label(root,text="Fee").place(x=10,y=120)
e1 = Entry(root)
e1.place(x=140,y=10)
e2 = Entry(root)
e2.place(x=140,y=80)
e3 = Entry(root)
e3.place(x=140,y=120)
root.mainloop()
完全错误
sql语句中未使用所有参数
解决方法
要插入Entry
小部件中,可以使用类似insert()
的方法。但是,由于您从数据库中获取数据是在函数内部,并且您试图在函数外部访问数据,因此必须使其在global
范围内可用。
def Ok():
global myresult
studname = e1.get()
coursename = e2.get()
........
现在,您也可以从外部myresult
访问Ok()
。因此,您现在可以说出话来将其插入相应的输入框
e1.insert(0,myresult) #can be e2.insert(0,myresult) or e3.insert(0,myresult) depending on where you want to insert
您可以将其从代码中删除,因为它位于主代码块中,几乎可以在任何地方使用
global e1
global e2
global e3
此外,除此以外,我建议您使用参数替换代替串联execute
语句,因为这是一种更安全的方法。
mycursor.execute("SELECT * FROM record where id = ?",(studname,)) #would be %s for mysql version
在这里,?
是一个占位符,因为您正在使用mariadb
(正如注释中的错误所示),我建议您将mysql.connector
更改为mariadb
(因为您使用的是mariadb服务器而不是mysql服务器,因此某些语法确实有所不同),只需说
pip install mariadb
和
import mariadb
,您可以按照documentation here
如有任何疑问或错误,请告诉我。
欢呼
,您可以做这样的事情
root = Tk()
msg='hello world!'
w=Text(master=root,exportselection=0,width=20,height=3)
w.pack()
w.insert(END,msg)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。