Josiah Carlson wrote:
I know this is not related to the wxPython list but I guess
I will get an answer quicker here than search through piles
of documentation.How can I find the next available id from a sqlite database using pysqlite?
You shouldn't need to do that, sqlite supports auto-increment primary
key columns. But if you do, you can select the max from the column
and add one. It is not safe with respect to other things changing
data from underneath you, but it will work if you only have one
thread/process mucking with your data.
Beware, it implements auto increment poorly and arguably dangerously IMO. It just adds one to the last ID that exists, so if you remove the last row and then add another one, the new one will get the old row's ID (or earlier). So if your IDS are 1,2,3, and you remove 3 and add another row, it will get ID 3 (again). If they are 1,2,4 and you remove 4, a new row will get ID 3! (http://www.sqlite.org/faq.html#q1)
This will require you to be quite careful about removing all traces of references to rows by IDs, when a row is deleted. I have been bitten numerous times by this. Anyone know of a way to get actual unique IDs? I have been tempted to handle it myself, persisting the last row ID used somewhere and incrementing from that.
- Mike
···
On Thu, Jul 24, 2008 at 12:26 AM, Wesley Nitsckie > <wesleynitsckie@gmail.com> wrote: