Python sqlite3.ERROR:没有这样的表:部门

Python sqlite3.ERROR:没有这样的表:部门,python,sqlite,visual-studio-code,connection,operationalerror,Python,Sqlite,Visual Studio Code,Connection,Operationalerror,此错误突然出现,我确信数据库中存在这些表,因为在我不知道发生了什么之后,它工作得很好,窗口无法显示给我: 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\LE

此错误突然出现,我确信数据库中存在这些表,因为在我不知道发生了什么之后,它工作得很好,窗口无法显示给我:

 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
当我运行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()
employee.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
表格已经存在,我拍摄屏幕:


您是否创建了该表?请发布emloyee.py,这是导致错误的文件。连接到数据库,运行
.tables
并在此处发布输出也不会有什么坏处。是的,表已经存在了in@Sara,谢谢您的快速回复,但我要的是emloyee.py,不是employee.py。查看stacktrace:
文件“c:\Users\LENOVO\Desktop\HR\u project\HR\u System\controler\app\emloyee.py”,在get\u all\u emps
中的第22行,以及您从Emloye import Employee导入的:
。对stacktrace的进一步检查得出以下结论:
文件“c:\Users\LENOVO\Desktop\HR\u project\HR\u System\controler\app.py”,第14行,在uuu init\uuuuu.self.depts=Departments.get\u all\u emps()中
。您提供的app.py没有这行代码,它是self.depts=Departments.get\u all\u depts()
。要么你运行的是另一个app.py,发布了错误的stacktrace,要么是一个陈旧的app.pyc文件有点奇怪。
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