On 27 Oct 2011 at 22:04, Kees Nuyt <k.n...@zonnet.nl> 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.
> All processes have to behave, if there's one that does the SELECT
> above in uncommitted mode and proceeds to use it, you have a
> problem.
> So, better use AUTOINCREMENT.

Thanks for the various responses. I hadn't thought of the max(id) approach but 
I'm not sure I can put a BEGIN/COMMIT around everything, as there are network 
exchanges involved there too. Nothing at the SQLite web site gave me a better 
clue than my own approach, either, but it doesn't look like there's anything 
obvious I overlooked.

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

Reply via email to