I'm working with the sqlite3 library under python 3.5.
I have defined two tables, related through a foreign key, with the following structures:
Table 1 structure:
'Table1_ID' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE
'Name' TEXT
Table 2 structure:
'Table2_ID' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE
other fields
'NameID' INTEGER NOT NULL, FOREIGN KEY(`NameID`) REFERENCES table_1 ( Table1_ID )
Then I want to add a record on Table1, and subsequently add a record on Table2 that references the record just inserted in Table1. The steps would be:
Add record to Table1 using an INSERT query - the database engine will assign a unique Table1_ID integer automatically
Somehow read that Table1_ID value, say Table1_ID=821
Add a record to Table2 using an INSERT query, where I specify that NameID=821
Now, my question is about the most efficient way of implementing step 2. An obvious option is to execute a "SELECT" query to get the cursor over the just inserted record, and then read its Table1_ID value.
But I am wondering whether there is an alternative way of knowing Table1_ID juts after the INSERT record without having to execute a second query.