KeyError: 6 в месте, где все нормально. Что можно исправить?

Мне нужно запустить программу, но внезапно я столкнулся с этой ошибкой Я использовал YouTube в качестве источника. Не судите слишком строго

Терминал

2024-06-01 14:16:01,399 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,399 INFO sqlalchemy.engine.Engine 
            SELECT *
            FROM Payment

2024-06-01 14:16:01,400 INFO sqlalchemy.engine.Engine [generated in 0.00065s] ()
2024-06-01 14:16:01,401 INFO sqlalchemy.engine.Engine ROLLBACK
2024-06-01 14:16:01,401 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,401 INFO sqlalchemy.engine.Engine
            SELECT *
            FROM Room

2024-06-01 14:16:01,402 INFO sqlalchemy.engine.Engine [generated in 0.00059s] ()
2024-06-01 14:16:01,402 INFO sqlalchemy.engine.Engine ROLLBACK
{1: (1, 101), 2: (2, 102), 3: (3, 103), 4: (4, 104)}
2024-06-01 14:16:01,403 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,403 INFO sqlalchemy.engine.Engine
            SELECT *
            FROM Students

2024-06-01 14:16:01,404 INFO sqlalchemy.engine.Engine [generated in 0.00043s] ()
2024-06-01 14:16:01,404 INFO sqlalchemy.engine.Engine ROLLBACK
2024-06-01 14:16:01,404 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,405 INFO sqlalchemy.engine.Engine
            SELECT *
            FROM Gender

2024-06-01 14:16:01,406 INFO sqlalchemy.engine.Engine [generated in 0.00082s] ()
2024-06-01 14:16:01,406 INFO sqlalchemy.engine.Engine ROLLBACK
2024-06-01 14:16:01,406 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,406 INFO sqlalchemy.engine.Engine
            SELECT *
            FROM Status

2024-06-01 14:16:01,407 INFO sqlalchemy.engine.Engine [generated in 0.00048s] ()
2024-06-01 14:16:01,407 INFO sqlalchemy.engine.Engine ROLLBACK
2024-06-01 14:16:01,408 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2024-06-01 14:16:01,409 INFO sqlalchemy.engine.Engine
            SELECT *
            FROM "General information"
            WHERE (? = 0 OR Status_Id = ?)
            AND (? = 0 OR Gender_Id = ?)
            Order BY Room_Id

2024-06-01 14:16:01,410 INFO sqlalchemy.engine.Engine [generated in 0.00094s] (0, 0, 0, 0)
2024-06-01 14:16:01,411 INFO sqlalchemy.engine.Engine ROLLBACK
Traceback (most recent call last):
  File "e:\Учеба\Учеба\8 семестр\Диплом\PuthonPrograms\main.py", line 226, in <module>
    window = MainWindow()
             ^^^^^^^^^^^^
  File "e:\Учеба\Учеба\8 семестр\Диплом\PuthonPrograms\main.py", line 67, in __init__
    self.load_General_information()
  File "e:\Учеба\Учеба\8 семестр\Диплом\PuthonPrograms\main.py", line 131, in load_General_information
    Mame = self.student[r.Student_Id].Name

Это код

import sys
import os
import PySide6
from PySide6.QtWidgets import QApplication, QMainWindow, QDialog
from mainwindow import Ui_MainWindow
from edit_dialog import Ui_Dialog
from sqlalchemy import create_engine, text
from sqlalchemy.orm import Session


dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path


class EditDialog(QDialog):
    def __init__(self, gender, status,  *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.ui.btnAdd.clicked.connect(self.accept)
        self.ui.btnCancel.clicked.connect(self.reject)

        print(gender)
        for g in gender.values():
            self.ui.cmbGender.addItem(g.Name, g)
        print(status)
        for s in status.values():
            self.ui.cmbStatus.addItem(s.Name, s)

    # Те данные которые вводятся
    def get_data(self):
        return {
            "NumRoom": self.ui.txtNumRoom.text(),
            "Name": self.ui.txtName.text(),
            "Surname": self.ui.txtSurname.text(),
            "MidName": self.ui.txtMidName.text(),
            "PhoneNum": self.ui.txtPhoneNum.text(),
            "Date":self.ui.txtDate.text(),
            "Gender": self.ui.cmbGender.currentData().Id,
            "SumPay":self.ui.txtSumPay.text(),
            "Introduced":self.ui.txtIntroduced.text(),
            "Remains":self.ui.txtRemains.text(),
            "Status": self.ui.cmbStatus.currentData().Id
        }


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.engine = create_engine("sqlite+pysqlite:///DataBase.db", echo=True)
        
        
        self.load_payment()
        self.load_room()
        self.load_student()
        self.load_gender()
        self.load_status()
        self.load_General_information()

        self.ui.cmbStatusPay.currentIndexChanged.connect(self.load_General_information)
        self.ui.cmbGender.currentIndexChanged.connect(self.load_General_information)
        self.ui.btnAdd.clicked.connect(self.on_btnAdd_click)

    # Кнопка Добавить
    def on_btnAdd_click(self):
        dialog = EditDialog(self.gender, self.status)
        r = dialog.exec()
        if r == 0:
            return

        data = dialog.get_data()
        with Session(self.engine) as s:
            query = """
            INSERT INTO "General information"(Room_Id, Student_Id, Gender_Id, Payment_Id, Status_Id)
            VALUES(:rid, :sid, :gid, :pid, :statid)
            """

            s.execute(text(query), {
                "rid": data['NumRoom'],
                "sid": data['Name'],
                "sid": data['Surname'],
                "sid": data['MidName'],
                "sid": data['PhoneNum'],
                "sid": data['Date'],
                "gid": data['Gender'],
                "pid": data['SumPay'],
                "pid": data['Introduced'],
                "pid": data['Remains'],
                "statid": data['Status']
            })
            s.commit()
    
        

    def load_General_information(self):
        Payment_data = self.ui.cmbStatusPay.currentData()
        if Payment_data:
            Status_Id = self.ui.cmbStatusPay.currentData().Id
        else:
            Status_Id = 0

        Gender_data = self.ui.cmbGender.currentData()
        if Gender_data:
            Gender_Id = self.ui.cmbGender.currentData().Id
        else:
            Gender_Id = 0

        self.ui.lstItems.clear()

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM "General information"
            WHERE (:sid = 0 OR Status_Id = :sid)
            AND (:gid = 0 OR Gender_Id = :gid)
            Order BY Room_Id
            """

            rows = s.execute(text(query), {"sid": Status_Id, "gid": Gender_Id })
            for r in rows:
                NumRoom = self.room[r.Room_Id].NumRoom
# 131 line
                Name = self.student[r.Student_Id].Name
                Surname = self.student[r.Student_Id].Surname
                MidName = self.student[r.Student_Id].MidName
                PhoneNum = self.student[r.Student_Id].PhoneNum
                Date = self.student[r.Student_Id].Date
                Gender = self.gender[r.Gender_Id].Name
                SumPay = self.payment[r.Payment_Id].SumPay
                Introduced = self.payment[r.Payment_Id].Introduced
                Remains = self.payment[r.Payment_Id].Remains
                Status = self.status[r.Status_Id].Name
                
                self.ui.lstItems.addItem(f"{NumRoom} {Name} {Surname} {MidName} {PhoneNum} {Date} {Gender} {SumPay} {Introduced} {Remains} {Status}")

    def load_room(self):
        self.room = {}

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM Room
            """
            rows = s.execute(text(query))
            for r in rows:
                self.room[r.Id] = r

        print(self.room)

    def load_student(self):
        self.student = {}

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM Students
            """
            rows = s.execute(text(query))
            for s in rows:
                self.student[s.Id] = s

    def load_payment(self):
        self.payment = {}

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM Payment
            """
            rows = s.execute(text(query))
            for p in rows:
                self.payment[p.Id] = p            


    def load_status(self):
        self.status = {}

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM Status
            """
            rows = s.execute(text(query))
            for s in rows:
                self.status[s.Id] = s

        self.ui.cmbStatusPay.addItem("Статус оплаты")
        for s in self.status.values():
            self.ui.cmbStatusPay.addItem(s.Name, s)

    def load_gender(self):
        self.gender = {}

        with Session(self.engine) as s:
            query = """
            SELECT *
            FROM Gender
            """
            rows = s.execute(text(query))
            for g in rows:
                self.gender[g.Id] = g

        self.ui.cmbGender.addItem("Пол")
        for g in self.gender.values():
            self.ui.cmbGender.addItem(g.Name, g)

    
if __name__ == "__main__":
    # Объект приложения
    app = QApplication(sys.argv)        

    window = MainWindow()
    window.show()

    # Запуск приложения
    sys.exit(app.exec())     

Ответы (0 шт):