main.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import sys
  2. from PyQt6.QtWidgets import (
  3. QApplication, QWidget, QVBoxLayout, QHBoxLayout,
  4. QLineEdit, QPushButton, QTableWidget,
  5. QTableWidgetItem, QLabel
  6. )
  7. from bd import get_connection
  8. class SportApp(QWidget):
  9. def __init__(self):
  10. super().__init__()
  11. self.selected_id = None
  12. self.setWindowTitle("Спортивные организации города")
  13. self.setGeometry(200, 200, 900, 500)
  14. self.init_ui()
  15. self.load_data()
  16. def init_ui(self):
  17. layout = QVBoxLayout()
  18. # ===== Поля ввода =====
  19. self.name_input = QLineEdit()
  20. self.address_input = QLineEdit()
  21. self.sport_input = QLineEdit()
  22. self.phone_input = QLineEdit()
  23. layout.addWidget(QLabel("Название"))
  24. layout.addWidget(self.name_input)
  25. layout.addWidget(QLabel("Адрес"))
  26. layout.addWidget(self.address_input)
  27. layout.addWidget(QLabel("Вид спорта"))
  28. layout.addWidget(self.sport_input)
  29. layout.addWidget(QLabel("Телефон"))
  30. layout.addWidget(self.phone_input)
  31. # ===== Кнопки =====
  32. btn_layout = QHBoxLayout()
  33. self.add_btn = QPushButton("Добавить")
  34. self.update_btn = QPushButton("Изменить")
  35. self.delete_btn = QPushButton("Удалить выбранную")
  36. self.add_btn.clicked.connect(self.add_org)
  37. self.update_btn.clicked.connect(self.update_org)
  38. self.delete_btn.clicked.connect(self.delete_org)
  39. btn_layout.addWidget(self.add_btn)
  40. btn_layout.addWidget(self.update_btn)
  41. btn_layout.addWidget(self.delete_btn)
  42. layout.addLayout(btn_layout)
  43. # ===== Таблица =====
  44. self.table = QTableWidget()
  45. self.table.setColumnCount(5)
  46. self.table.setHorizontalHeaderLabels(
  47. ["ID", "Название", "Адрес", "Спорт", "Телефон"]
  48. )
  49. self.table.itemClicked.connect(self.row_selected)
  50. layout.addWidget(self.table)
  51. self.setLayout(layout)
  52. # ===== Загрузка данных =====
  53. def load_data(self):
  54. conn = get_connection()
  55. cur = conn.cursor()
  56. cur.execute("""
  57. SELECT id, name, address, sport_type, phone
  58. FROM sport_organizations
  59. ORDER BY id
  60. """)
  61. rows = cur.fetchall()
  62. cur.close()
  63. conn.close()
  64. self.table.setRowCount(len(rows))
  65. for i, row in enumerate(rows):
  66. for j, value in enumerate(row):
  67. self.table.setItem(i, j, QTableWidgetItem(str(value)))
  68. # ===== Выбор строки =====
  69. def row_selected(self):
  70. row = self.table.currentRow()
  71. self.selected_id = int(self.table.item(row, 0).text())
  72. self.name_input.setText(self.table.item(row, 1).text())
  73. self.address_input.setText(self.table.item(row, 2).text())
  74. self.sport_input.setText(self.table.item(row, 3).text())
  75. self.phone_input.setText(self.table.item(row, 4).text())
  76. # ===== Добавление =====
  77. def add_org(self):
  78. conn = get_connection()
  79. cur = conn.cursor()
  80. cur.execute("""
  81. INSERT INTO sport_organizations
  82. (name, address, sport_type, phone)
  83. VALUES (%s, %s, %s, %s)
  84. """, (
  85. self.name_input.text(),
  86. self.address_input.text(),
  87. self.sport_input.text(),
  88. self.phone_input.text()
  89. ))
  90. conn.commit()
  91. cur.close()
  92. conn.close()
  93. self.load_data()
  94. # ===== Обновление =====
  95. def update_org(self):
  96. if self.selected_id is None:
  97. return
  98. conn = get_connection()
  99. cur = conn.cursor()
  100. cur.execute("""
  101. UPDATE sport_organizations
  102. SET name=%s,
  103. address=%s,
  104. sport_type=%s,
  105. phone=%s
  106. WHERE id=%s
  107. """, (
  108. self.name_input.text(),
  109. self.address_input.text(),
  110. self.sport_input.text(),
  111. self.phone_input.text(),
  112. self.selected_id
  113. ))
  114. conn.commit()
  115. cur.close()
  116. conn.close()
  117. self.load_data()
  118. # ===== Удаление =====
  119. def delete_org(self):
  120. if self.selected_id is None:
  121. return
  122. conn = get_connection()
  123. cur = conn.cursor()
  124. cur.execute(
  125. "DELETE FROM sport_organizations WHERE id=%s",
  126. (self.selected_id,)
  127. )
  128. conn.commit()
  129. cur.close()
  130. conn.close()
  131. self.load_data()
  132. # ===== запуск =====
  133. if __name__ == "__main__":
  134. app = QApplication(sys.argv)
  135. window = SportApp()
  136. window.show()
  137. sys.exit(app.exec())