0

If I like to insert something into a table 'things' and a first reference to it in another table 'thing_tags', how can I make sure this is atomically done?

Using two queries, I would do it like this:

INSERT INTO things(thing) VALUES("a thing");
INSERT INTO thing_tags(thing_id,tag) VALUES(SELECT MAX(rowid) FROM things), "a default tag");

However, this makes to queries, I am not sure if some other query could go inbetween, and it looks useless complicated. Is there a simple and clean way to insert the tag referencing the new thing?

dronus
  • 10,774
  • 8
  • 54
  • 80
  • Possible duplicate of [How can I access the last inserted row ID within a SQL script?](http://stackoverflow.com/questions/1822135/how-can-i-access-the-last-inserted-row-id-within-a-sql-script) – mu is too short Jan 09 '17 at 02:22
  • This should not be done from inside SQL. What language are you using to access SQLite? – CL. Jan 09 '17 at 08:59

0 Answers0