0

I'm trying to get primary keys from table 1 (patient_id) & table 2 (assessor_id), and insert them automatically into table 3. My HTML code for table 3 isn't working since it requires primary keys from other tables. I have to input the values (i.e.: patient_id, and assessor_id) manually. Is there any ways to insert the primary key automatically? Thanks.

  • 1
    Or rather, actually you just need `SELECT LAST_INSERT_ID()` to get the ID after each `INSERT`. You should do all of this in a transaction. https://stackoverflow.com/questions/2548493/how-do-i-get-the-id-after-insert-into-mysql-database-with-python – Bailey Parker Mar 21 '19 at 14:48
  • where should i add "select last insert id ()" @BaileyParker? i tried to replace it with "patient_id" but it doesn't work – user8786892 Mar 21 '19 at 15:03
  • Immediately after the first insert, just save `partient_id = cursor.lastrowid`. Then after the second save `assessor_id = cursor.lastrowid`. Then your final insert just works. – Bailey Parker Mar 21 '19 at 15:06
  • @BaileyParker I got this error after doing that: 'Cursor' object has no attribute 'lastrowid'. – user8786892 Mar 21 '19 at 15:15
  • Then `g.db_conn.insert_id()`. Read the linked duplicates. They will help. – Bailey Parker Mar 21 '19 at 15:16
  • thanks, it works now :) – user8786892 Mar 21 '19 at 15:34

0 Answers0