Thanks for that. For now, any deletions would be "en masse" so gaps wouldn't be 
a problem, but the max() idea might be useful later.


Sent from my Samsung Galaxy S7 - powered by Three
-------- Original message --------From: Clemens Ladisch <[email protected]> 
Date: 08/03/2017  08:59  (GMT+00:00) To: [email protected] 
Subject: Re: [sqlite] Is this safe use of SELECT in an INSERT? 
Graham Holden wrote:
> insert or ignore into Servers values (    'MyServer',    12345,    ( select 
> count (*) from Servers ) )
>
> is the "count (*)" guaranteed to be the count before the insert?

Yes.  But if any row was ever deleted, that value might alread by used.

You could make serverIdx autoincrementing by making it the INTEGER
PRIMARY KEY.

If you do not want to change the primary key, you can get a value that
is guaranteed to be unused with max():

INSERT OR IGNORE INTO Servers
VALUES ('MyServer', 12345,
        (SELECT ifnull(max(serverIdx), 0) + 1 FROM Servers));


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
[email protected]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to