SQLite- elegantly setting IDs
I want to use a database like:
table wrongdoer (primarykey integer ID, date)
table crime (numeric foreign_key_to_wrongdoer, text crime)
I want my application (in python) to register a wrongdoer (which should give the entry a unique integer) and register the first crime against him. My idea is rather clumsy:
insert into wrongdoer(...)
id=cur.execute("select max(ID)") //this is to select the most recent ID
cur.execute("insert into crime('"+id+"'"crime+"'")")
That is, insert an entry, select i开发者_如何学Pythonts unique key from DB (assuming its the highest number), and then use it for the subsequent queries. Is there a better way to do it?
Check this function out: http://www.sqlite.org/c3ref/last_insert_rowid.html
精彩评论