python - 未在 SQL 数据库中插入 GUI 文本输入字段
问题描述
我正在尝试将文本输入字段中的输入获取到 SQL 中的表中。但是,当我插入该值时,我的数据库中看不到任何结果。我没有收到任何错误,所以我似乎找不到错误。
#place the text entry fields
global firstname
global lastname
global mark__
global targetgrade
firstname = Entry(self, width = 15, bg = "white")
firstname.grid(row = 1, column=1, padx =5, pady = 5)
lastname = Entry(self, width = 15, bg = "white")
lastname.grid(row = 2, column=1, padx =5, pady = 5)
mark__ = Entry(self, width = 15, bg = "white")
mark__.grid(row = 3, column=1, padx =5, pady = 5)
targetgrade = Entry(self, width = 15, bg = "white")
targetgrade.grid(row = 4, column=1, padx =5, pady = 5)
#Buttons to navigate
button1 = tk.Button(self, text = "Submit",
command = lambda:controller.show_frame(Submit) )
button1.grid(row = 8 , column= 0)
button2 = tk.Button(self, text = "Proceed",
command = lambda:controller.show_frame(ProceedToAnalysis) )
button2.grid(row = 8, column = 1)
类提交(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Entry Submitted", font = LARGE_FONT)
label.grid(pady = 10, padx = 10)
button1 = tk.Button(self, text = "Back to Home",
command = lambda:controller.show_frame(StartPage) )
button1.grid(row = 8 , column= 0)
button2 = tk.Button(self, text = "Proceed",
command = lambda:controller.show_frame(ProceedToAnalysis) )
button2.grid(row = 8, column = 1)
#inserting into database
global firstname
global lastname
global mark__
global targetgrade
first = firstname.get()
last= lastname.get()
mark = mark__.get()
target_grade = targetgrade.get()
percentage =0
grade = "Not yet declared"
cur.execute("INSERT INTO class (first, last, mark, target_grade, percentage,grade) VALUES (?,?,?,?,?,?)",(first,last,mark,target_grade,percentage,grade))
conn.commit() #inserting the values in the database
firstname.delete(0,END) # clearing the entry after the user has inputted data
lastname.delete(0,END)
mark__.delete(0,END)
targetgrade.delete(0,END)
firstname.focus_set()
我已经完成了另一个具有相同功能的不太复杂的 tkinter 代码的版本,并且似乎按应有的方式添加到数据库中。我对这段代码做了同样的事情,但它似乎不起作用。先感谢您 :)
#my previous code which works
def submit():
first = firstname.get()
last= lastname.get()
mark = mark__.get()
target_grade = targetgrade.get()
percentage =0
grade = "Not yet declared"
cur.execute("INSERT INTO class (first, last, mark, target_grade, percentage,grade) VALUES (?,?,?,?,?,?)",(first,last,mark,target_grade,percentage,grade))
conn.commit() #inserting the values in the database
...
复制评论:我试过了
def insertintosql(first,last,mark,target_grade,percentage,grade): with conn: cur.execute("INSERT INTO class (first, last, mark, target_grade, percentage,grade) VALUES (?,?,?,?,?,?)", (first,last,mark,target_grade,percentage,grade) )
解决方案
You might want to look at changing the conn.commit()
statement to a cur.commit()
statement to see if that works. If it does, it means your connection definition isn't leveraging autocommit, and you need to explicitly commit the insert. Not sure what is different between this code and your more complex tkinter code, though.
推荐阅读
- c# - 如何断言已抛出异常
- java - JPA 查询中的 NullPointerException
- c# - 自定义排序字典
使用 LINQ 向下遍历 - search - 为什么 indexed_search 仅在 TYPO3 8 / 9 的子页面上不起作用?
- node.js - 是否可以使用 js-clien 运行 Inifinispan 连续查询
- excel - 基于正则表达式的 Excel“智能”自动完成(新功能?)
- php - 如何在 PHP 中将 GROUP BY + ORDER BY + WHERE 与 GET 一起使用?
- java - 如何在 try catch throw 中抛出错误对话框?
- .net - 关联 dotnet 核心服务之间的 Web 请求
- html - Angular 6 IE 样式内联