开发者

SQLite- elegantly setting IDs

开发者 https://www.devze.com 2023-02-18 01:08 出处:网络
I want to use a database like: table wrongdoer (primarykey integer ID, date) table crime (numeric foreign_key_to_wrongdoer, text crime)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消