On 27 Oct 2011, at 10:04pm, Kees Nuyt wrote:

> On Thu, 27 Oct 2011 21:47:17 +0100, Simon Slavin
> <slav...@bigfraud.org> wrote:
> 
>> On 27 Oct 2011, at 8:12pm, Tim Streater wrote:
>> 
>>> Is there a way to get a unique id from a database
>>> without actually creating a new row?
>> 
>> Sure.  Do something like
>> 
>> SELECT max(id) FROM mytab;
>> 
>> Then in your own code, add 1 to it and use that as
>> the "id" for the row you're about to save. 
>> Make sure you handle the NULL case (where mytab
>> doesn't have any rows in it yet) correctly.
> 
> And wrap it all in an IMMEDIATE or EXCLUSIVE transaction, or
> you'll get a race condition if some other process tries to do the
> same at the same time. 

Oh right.  Yes, if you have more than one process doing this at the same time, 
you're screwed.  Better just to pick a random integer out of a huge range and 
use that.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to