есть оконное приложение, где в элемент QLineEdit вводятся различные данные (айди, кличка, пол, порода).
вот часть кода, где происходит парсинг и вставка в бд:
def add_row_to_table(self, animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date):
row_position = self.animals_table.rowCount()
self.animals_table.insertRow(row_position)
item_animal_id = QTableWidgetItem(str(animal_id))
item_animal_id.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 0, item_animal_id)
item_name = QTableWidgetItem(name)
item_name.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 1, item_name)
item_gender = QTableWidgetItem(gender)
item_gender.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 2, item_gender)
item_age = QTableWidgetItem(age)
item_age.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 3, item_age)
item_species = QTableWidgetItem(species)
item_species.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 4, item_species)
item_reserve = QTableWidgetItem(reserve)
item_reserve.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 5, item_reserve)
item_date_of_receipt = QTableWidgetItem(date_of_receipt)
item_date_of_receipt.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 6, item_date_of_receipt)
item_pickup_date = QTableWidgetItem(pickup_date)
item_pickup_date.setTextAlignment(Qt.AlignCenter)
self.animals_table.setItem(row_position, 7, item_pickup_date)
def add_animal(self):
animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date, ok = self.get_input()
if ok:
self.add_row_to_table(animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date)
self.insert_into_database(animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date)
def get_input(self):
dialog = QDialog(self)
dialog.setWindowTitle('добавление животного')
dialog.resize(700, 100)
animal_id_input = QLineEdit()
name_input = QLineEdit()
gender_input = QLineEdit()
age_input = QLineEdit()
species_input = QLineEdit()
reserve_input = QLineEdit()
date_of_receipt_input = QLineEdit()
pickup_date_input = QLineEdit()
animal_id_label = QLabel('id:')
name_label = QLabel('имя:')
gender_label = QLabel('вид:')
age_label = QLabel('возраст:')
species_label = QLabel('порода:')
reserve_label = QLabel('зарезервирован(а):')
date_of_receipt_label = QLabel('дата поступления:')
pickup_date_label = QLabel('дата забора:')
font = QFont('Gertrude', 9)
animal_id_label.setFont(font)
name_label.setFont(font)
gender_label.setFont(font)
age_label.setFont(font)
species_label.setFont(font)
reserve_label.setFont(font)
date_of_receipt_label.setFont(font)
pickup_date_label.setFont(font)
layout = QFormLayout()
layout.addRow(animal_id_label, animal_id_input)
layout.addRow(name_label, name_input)
layout.addRow(gender_label, gender_input)
layout.addRow(age_label, age_input)
layout.addRow(species_label, species_input)
layout.addRow(reserve_label, reserve_input)
layout.addRow(date_of_receipt_label, date_of_receipt_input)
layout.addRow(pickup_date_label, pickup_date_input)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
def handle_close():
dialog.deleteLater()
button_box.accepted.connect(handle_close)
button_box.rejected.connect(handle_close)
layout.addWidget(button_box)
dialog.setLayout(layout)
if dialog.exec_() == QDialog.Accepted:
animal_id = animal_id_input.text().strip()
name = name_input.text().strip()
gender = gender_input.text().strip()
age = age_input.text().strip()
species = species_input.text().strip()
reserve = reserve_input.text().strip()
date_of_receipt = date_of_receipt_input.text().strip()
pickup_date = pickup_date_input.text().strip()
return animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date, True
else:
return '', '', '', '', '', '', '', '', False
def insert_into_database(self, animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date):
try:
connection = psycopg2.connect(
host='localhost',
port=5432,
database='test',
user='postgres',
password='4r5t2w1q'
)
cursor = connection.cursor()
cursor.execute('INSERT INTO cats_admin (id, name, gender, age, species, reserve, date_of_receipt, pickup_date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
(animal_id, name, gender, age, species, reserve, date_of_receipt, pickup_date))
connection.commit()
connection.close()
except Exception as e:
print(f'ошибка добавления в базу данных: {e}')
но потом просто ничего не происходит. новая запись не появляется ни в таблице приложения, ни в базе данных, ошибок не появляется. подскажите, как сделать правильно?
p.s тип даннных у столбца "id" в базе данных - integer, может проблема в этом?