How to resolve Sqlite auto increment gap?

Asked

Viewed 198 times

1

I have a code that erases one Row whole of a database, but there is a problem because I get a hole getting an ID without data (what I deleted) for this being autoincrement, does not delete the ID value. How to solve the problem?

  • 1

    That’s not supposed to be a problem. What do you need sequential id’s for? In most cases id’s matter that they are unique, not sequential.

  • It makes no sense for the id of a table to be continuous. If the application needs to know for some reason the position of a particular line, just take the cursor position on this line.

1 answer

4


There’s nothing wrong with that, unless you’ve put together a system that requires it, which is a terrible idea, and you should review this. Ids should be unique and sequential, but not necessarily continuous.

Even if they were bills that required continuity, the number should be used as ID and invoices could not be removed, which is worth anything. Everything that should be continuous should not be removed. It is possible to mark as invalid line for use, but leave it there.

If you still want to take advantage of removed Ids, which I do not advise, you can use the marker mentioned above and throughout query filter what is logically removed and create a table called freelist and store there the Ids that way removed, there before making a normal insert checks if there is an ID there, if you have use it to update the line, taking care to clean everything, instead of inserting a new line. If you do not have free Ids, do a normal insert. If you do not know how to do this right you will have a running condition or you may have problems with references to the previously used ID. It’s so much work to make sure it’s better not to.

  • then, if Voce needs to have sequential numbers anyway, hence, Voce can create an autoincrement column to use as a unique table id and another to use with bkp, hence, each time you change the rows of the Voce table rearrange the values in the bkp column from there, Voce will always have sequential numbers in that column and the ID column itself gets the values it wants, so Voce will always have a sequence.

  • but remember, if Voce changes the values in that column, and Voce already has a reference to a row that there was a value with the previous key, Voce will consequently change this reference, because the value of the key that Voce was using now will be another, 'cause Voce rearranged the values in that column, so that was said here, that Naum is a good idea

  • dai, if for example Voce had the line "ID=3,bkp=3, name=pedro" and "ID=4,bkp=4, name=Joao" and "ID=5,bkp=5, name=maria" dai Voce deletes the line id=4 and rearrange bkp will become bkp(5) = 4, so the name that Voce was using in the application will appear as maria, but was to appear as Joao because of the previous reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.