我的目标是学习创建一个小部件,它可以在一个简单的employeedb上执行Create/Read/Update/Delete(CRUD)操作
我附加了两个文件widget.py和dbcalls.py,第二个文件与sqlite db交互。py文件创建小部件,通过文本框和相关按钮从用户接收数据
我正在用employeeId、employeeName和employeeSalary对dbcalls.Create()函数进行Create调用。但是这些值在dbcalls中没有收到。Create函数不确定原因(参数传递不起作用)
#File1 - widget.py
import tkinter as tk
from dbcalls import *
class Application(tk.Frame):
eId = ""
eName = ""
eSalary = ""
empData = [eId,eName,eSalary]
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.grid()
self.createWidgets()
def Create(self):
self.getData()
print ("Before odbc.create()")
self.odbc.Create(self.eId,self.eName,self.eSalary)
# def Read(self):
# self.odbc.Read(empData)
#def Update(self):
# self.odbc.Update(empData)
#def Delete(self):
# self.odbc.Delete(empData)
def getData(self):
self.odbc = dbcalls()
eId = str(self.eIdValue.get())
eName = str(self.eNameValue.get())
eSalary = str(self.eSalaryValue.get())
empData = [eId,eName,eSalary]
print ("empData is ", empData)
def createWidgets(self):
#empData Details
self.eIdLabel = tk.Label(self, text='Employee Id')
self.eIdValue= tk.Entry(self)
self.eIdLabel.grid(row=0,column=0)
self.eIdValue.grid(row=0,column=1)
self.eNameLabel = tk.Label(self,text='Employee Name')
self.eNameValue = tk.Entry(self)
self.eNameLabel.grid(row=1,column=0)
self.eNameValue.grid(row=1,column=1)
self.eSalaryLabel = tk.Label(self, text='Employee Salary')
self.eSalaryValue= tk.Entry(self)
self.eSalaryLabel.grid(row=2,column=0)
self.eSalaryValue.grid(row=2,column=1)
#CRUD Buttons
#odbc = dbcalls()
self.CreateButton = tk.Button(self,text='Create', command=self.Create)
self.CreateButton.grid(row=3,column=0)
#self.ReadButton = tk.Button(self,text='Read', command= self.Read)
#self.ReadButton.grid(row=3,column=1)
#self.UpdateButton = tk.Button(self,text='Update', command=self.Update)
#self.UpdateButton.grid(row=3,column=2)
#self.DeleteButton = tk.Button(self,text='Delete', command=self.Delete)
#self.DeleteButton.grid(row=3,column=3)
#self.ExitButton = tk.Button(self,text='Exit', command=self.Exited)
#self.ExitButton.grid(row=3,column=4)
def Exited(self):
print ('Exit Button Pressed')
#print (self.employee)
self.quit()
c = Application()
c.master.title('CRUD Operations')
c.mainloop()
# dbcalls.py File Below
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 16:06:12 2019
@author: CGDELL23
"""
import sqlite3
from sqlite3 import Error
import sys
class dbcalls:
def __init__(self):
self.eId = ""
self.eName = ""
self.eSalary = ""
self.empData = [self.eId, self.eName,self.eSalary]
def createConn(self):
try:
conn = None
db = "C:\\Users\CGDELL23\Desktop\python\wm910.db"
conn = sqlite3.connect(db)
except Error as e:
print (e)
return conn
def closeConn(self,conn):
conn.close
def Read(self,eId,eName,eSalary):
cN=self.createConn()
cR=cN.cursor()
cR.execute("select * from Employee where employee =?", eId)
rows = cR.fetchmany(100)
for row in rows:
print (rows)
self.closeConn(cN)
def Create(self,eId,eName,eSalary):
self.eId = eId
self.eName = eName
self.eSalary = eSalary
print ("In dbcalls.Create()",self.empData)
cN=self.createConn()
try:
cR=cN.cursor()
cR.execute("insert into Employee (EmployeeId,EmployeeName, EmployeeSalary) values (?,?,?) ", ('CG007','Patnam','450000'))
print ("Before Commit")
cN.commit()
print ("After Commit")
self.closeConn(cN)
except Error as e:
print (e)
sys.exit(1)
def Update(self,eId,eName,eSalary):
cN=self.createConn()
cR=cN.cursor()
cR.execute("update Employee SET EmpName = %s, EmpSalary=%s WHERE EmpId = %s", eName,eSalary, eId)
cN.commit()
self.closeConn(cN)
def delete(self,eId):
cN=self.createConn()
cR=cN.cursor()
cR.execute("delete from Employee where empId = %s", eId)
cN.commit()
self.closeConn(cN)
As you can see below in the dbcalls.Create() function the empData is all nulls. Even though the EmployeeId,EmployeeName and EmployeeSalary were successfully received in Create function of the Application class and passed to the Create function in the dbcalls class.
不确定为什么应用程序类中的参数没有传递给dbcalls类。
>>>runfile('C:/Users/CGDELL23/CRUDperations/widget.py', wdir='C:/Users/CGDELL23/CRUDOperations')
empData is ['CG007', 'Patnam', '450000']
Before odbc.create()
In dbcalls.Create() ['', '', '']
UNIQUE constraint failed: Employee.EmployeeId
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
这与“其他文件”无关
eId
etc在应用程序内部的类级别定义为空字符串,并且从不更新不过,您似乎根本不需要这些属性作为类或实例属性。您可能应该直接从
getData
返回它们相关问题 更多 >
编程相关推荐