0

这个错误突然出现,我确定数据库中存在表,因为它工作得很好,之后我不知道窗口无法显示给我:

 Traceback (most recent call last):
  File "c:\Users\LENOVO\Desktop\HR_project\HR_System\controler\app\app.py", line 71, in <module>
    window=MainWindow()
  File "c:\Users\LENOVO\Desktop\HR_project\HR_System\controler\app\app.py", line 12, in __init__
    self.depts=Departments.get_all_depts()
  File "c:\Users\LENOVO\Desktop\HR_project\HR_System\controler\app\departments.py", line 18, in get_all_depts    
    result= cur.execute(sql).fetchall()
sqlite3.OperationalError: no such table: departments

这是部门.py:

from sqlite3 import connect
class Departments:
    def __init__(self,dept_id,dept_name,location_id):
        self.dept_id=dept_id
        self.dept_name=dept_name
        self.location_id=location_id

    def __repr__(self):
        return self.dept_name

    @staticmethod    
    def get_all_depts():
        path="C:\\Users\\LENOVO\\Desktop\\HR_project\\HR_System\\controler\\app\\hr.db"

        with connect(path)as conn:
            cur=conn.cursor()
            sql= 'SELECT * FROM departments'
            result= cur.execute(sql).fetchall()
            result=[Departments(*row)for row in result]
            return result

    def saveToDb(self):
        path="C:\\Users\\LENOVO\\Desktop\\HR_project\\HR_System\\controler\\app\\hr.db"
        with connect(path)as conn:
            cur=conn.cursor()
            sql='INSERT  INTO departments VALUES (:dept_id , :dept_name , :location_id)'
            cur.execute(sql,self.__dict__)
            conn.commit()
        
    def deleteFromDb(self):
            path="C:\\Users\\LENOVO\\Desktop\\HR_project\\HR_System\\controler\\app\\hr.db"
            with connect(path)as conn:
                cur=conn.cursor()
                sql='DELETE FROM departments WHERE department_id = :dept_id'
                cur.execute(sql,self.__dict__)
                conn.commit()

当我运行它时,这里的 app.py 出现了上面的错误:

from PyQt5.uic import loadUi
from PyQt5.QtWidgets import *
from main_window import Ui_Form
from departments import Departments
from locats import locates
from emloyee import Employee
class MainWindow(QWidget,Ui_Form):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)

        self.depts=Departments.get_all_depts()
        self.load_depts()
        self.emps= Employee.get_all_emps()
        self.load_emps()
        self.cb_depts.currentIndexChanged.connect(self.select_depts)
        self.le_search.textChanged.connect(self.search)
        self.bt_add_dept.clicked.connect(self.show_add_depts_dialog)
        self.bt_del_dept.clicked.connect(self.delet_dept)

    def load_depts(self):
         dept_names= [d.dept_name for d in self.depts]
         self.cb_depts.addItems(dept_names)
    def load_emps(self):
        self.tb_emps.setRowCount(0)
        for i,e in enumerate(self.emps):
             self.tb_emps.insertRow(i)   
             for n,v in enumerate(e.__dict__.values()):    
                   self.tb_emps.setItem(i,n,QTableWidgetItem(str(v)))

    def select_depts(self,idx):
        self.load_emps()
        if idx !=0:
            dept_i=self.depts[idx-1]
            for i,e in enumerate(self.emps):
                 if e.dept_id != dept_i.dept_id:
                     self.tb_emps.hideRow(i)
                    
    def search(self):
        self.load_emps()
        text=self.le_search.text()
        if text != "":
            for i,e in enumerate (self.emps):
                if not e.emp_name.startswith(text):
                     self.tb_emps.hideRow(i)

    def show_add_depts_dialog(self):
       dialog = loadUi("C:\\Users\\LENOVO\\Desktop\\HR_project\\HR_System\\controler\\app\\add_depts.ui")    
        locs={str(l.location_id) for l in self.depts}
        dialog.cb_locats.addItems(locs)
        choice =dialog.exec()

        if choice ==1:
            dept = Departments(dialog.le_deps_id.text(),
                    dialog.le_deps_name.text(),
                    dialog.cb_locats.currentText())
            self.depts.append(dept)
            self.cb_depts.addItem(dept.dept_name) 

            dept.saveToDb()

    def delet_dept(self):
        idx=self.cb_depts.currentIndex()
        if idx != 0:
           self.cb_depts.removeItem(idx)
            dept = self.depts.pop(idx-1)
            dept.deleteFromDb()
        
app = QApplication([])
window=MainWindow()
window.show()
app.exec()

员工.py:

from sqlite3 import connect
class Employee:
    def __init__(self,emp_id,emp_name,email,hire_date,job_id,salary,dept_id):
        self.emp_id=emp_id
        self.emp_name=emp_name
        self.email=email
        self.hire_date=hire_date
        self.job_id=job_id
        self.salary=salary
        self.dept_id=dept_id
        
    def __repr__(self):
        return self.emp_name

    @staticmethod    
    def get_all_emps():
        path="C:\\Users\\LENOVO\\Desktop\\HR_project\\HR_System\\controler\\app\\hr.db"

        with connect(path)as conn:
            cur=conn.cursor()
            sql= 'SELECT employee_id,last_name,email,hire_date,job_id,salary,department_id FROM employees'
            result= cur.execute(sql).fetchall()
            result=[Employee(*row)for row in result]
            return result

表格已经存在我进行屏幕截图: 在此处输入图像描述

任何帮助请???????????????????????????????????????????????? ???

4

0 回答 0