我有两个表。
Table "public.part"
Column | Type |
-------------------+------------------------+
id | bigint |
part_name | character varying(255) |
part_department | character varying(255) |
part_manufacturer | character varying(255) |
part_model | character varying(255) |
part_category | character varying(255) |
part_storage | character varying(255) |
part_description | character varying(255) |
part_stocklimit | smallint |
Table "public.entry"
Column | Type |
----------------+------------------------+
id | bigint |
part_id | bigint |
entry_status | character(5) |
entry_qty | smallint |
entry_desc | character varying(500) |
entry_date | date |
entry_employee | character varying(225)
我想在entry TABLE中插入数据。entry TABLE中的part_id是part TABLE中id的FOREIGN KEY。
我使用PyQt5建立了一个from来插入条目TABLE的值,并使用psycopg2在python中运行psql命令,它看起来像这样。
名称标签有combobox,它从part TABLE中获取部件列表。现在我想从comboBox中插入与部件TABLE中id匹配的数据。所以让我们说像这样
INSERT INTO entry (part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee) VALUES
( SELECT id FROM part WHERE part_name=entry_part_name, entry_status, entry_qty, entry_desc, entry_date, entry_employee );
现在我已经在python中找出了标准的INSERT语句,它看起来是这样的,但我不知道如何匹配零件的id,使它能够插入id而不是零件名称。但我不知道如何匹配零件的id,使其能够插入id而不是零件名称。
def AddEntry_Data(self):
self.db = ConnectDatabase()
entry_part_name = self.comboBoxNewEntryPart.currentText()
entry_employee = self.comboBoxNewEntryEmployee.currentText()
entry_status = self.comboBoxNewEntryStatus.currentText()
entry_quantity = self.spinBoxNewEntryQuantity.value()
entry_description = self.lineNewEntryDescription.toPlainText()
entry_date = self.dateNewEntryDate.text()
try:
self.cur = self.db.cursor()
self.cur.execute('SAVEPOINT SP1')
self.cur.execute('''INSERT INTO entry (part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee)
VALUES (%s , %s , %s , %s , %s , %s , %s , %s)''', (entry_part_name, entry_status, entry_quantity, entry_description, entry_date, entry_employee))
except Exception as error:
self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
self.AddEntry_reset()
print(error)
# self.error_popup("Input Error", error)
else:
self.cur.execute('RELEASE SAVEPOINT SP1')
self.db.commit()
self.AddEntry_reset()
self.success_popup("Entry", "New Entry Added.")
self.db.close
我能想到的方法之一是选择查询id并存储在一个变量中,然后将该变量传递给insert查询。
try:
self.cur = self.db.cursor()
self.cur.execute('SAVEPOINT SP1')
self.cur.execute('''SELECT id FROM part WHERE part_name = %s ''', [(entry_part_name,)])
except Exception as error:
self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
self.AddEntry_reset()
self.error_popup("Input Error", error)
else:
fetched_part_id = self.cur.fetchone()
try:
self.cur.execute('''INSERT INTO entry (fetched_part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee)
VALUES (%s , %s , %s , %s , %s , %s)''', (fetch_id, entry_status, entry_quantity, entry_description, entry_date, entry_employee))
except Exception as error:
self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
self.AddEntry_reset()
self.error_popup("Input Error", error)
else:
self.cur.execute('RELEASE SAVEPOINT SP1')
self.db.commit()
self.AddEntry_reset()
self.success_popup("Entry", "New Entry Added.")
self.db.close()
如果有其他简单的方法来结合选择查询内插入查询在python中,像明智的sql命令说在顶部是感激。谢谢你。