Topic: ID in new record without to use counter

Can someone help me? The code below doesn't work, because when the record is deleted, sqlite ignores ID that has already been deleted. For example: ID 200 deleted. The statement "sqlexecute('select max(id) from people') + 1" will generate an ID 200, but sqlite will provide ID 201. Is there any routine that informs which new ID will be provided by sqlite?


procedure form2_OnShow (Sender: string; Action: string);
begin

  if action <> 'NewRecord' then form2.edit1.Value:= sqlexecute('select id from people where id =' +inttostr(form1.tablegrid1.dbitemid));
  if action = 'NewRecord' then form2.edit1.value:= sqlexecute('select max(id) from people') + 1;

end;
Roberto Alencar

Re: ID in new record without to use counter

jrga wrote:

Can someone help me? The code below doesn't work, because when the record is deleted, sqlite ignores ID that has already been deleted. For example: ID 200 deleted. The statement "sqlexecute('select max(id) from people') + 1" will generate an ID 200, but sqlite will provide ID 201. Is there any routine that informs which new ID will be provided by sqlite?


таблица sqlite_master  хранит информацию

SELECT name FROM sqlite_master WHERE type=table  ORDER BY name 

Re: ID in new record without to use counter

Hi Jrga, Vladimir,
Have a look at the script in the attachment to see how to get the next table sequence ID.
The 'sqlite_master' and sqlite_sequence tablegrids on Form1 are not needed - they just try to clarify how it works.
Derek.

Post's attachments

Attachment icon jrga.zip 337.29 kb, 88 downloads since 2023-08-27 

4 (edited by jrga 2023-08-28 21:23:54)

Re: ID in new record without to use counter

Derek and Pavlenko, muito obrigado e parabéns a vocês dois.

Roberto Alencar