Android SQLite database removing a record - android

I am now building an android phone book app and employing the method of SQLite database to store records. In my database, there is _id column which is a primary key and will increase automatically. A strange thing is that when I delete records, the removed _id number is not re-used.
E.g. Originally the database is
_id name
1 Peter
2 Mary
After deleting 2 (Mary), when I add a new record (John), I found that John is placed with an id = 3.
So my question is: is Mary still there? Is it just flagged to be "deleted" but not really deleted? If so, would this be a wastage of memory and should I do something to remove it completely?

The row is deleted. Generating the auto increment ID is implementation specific. It is upto to the autoincrement algorithm to assign the new id or reuse the deleted ID. Please refer sqlite tutorial : http://sqlite.org/autoinc.html
This is a quote in the mentioned in above link:
With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the automatically generated ROWIDs are guaranteed to be monotonically increasing.
Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. One is the usual increment. However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence. AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential.

No, after deleting row id 2, the record is completely deleted. The incrementing counter will continue from where it left off, so it is correct to see John with an id of 3.

You need to make your ID column an INT primary key and add auto increment to it.

Related

How exactly SQLite database update primary key id?

Suppose I have a table contacts
id Name Contact_No
-----------------------------------------
1 abc 12345
2 lmn 56784
3 pqr 83654
4 uvw 17637
5 xyz 98345
If I delete row 3 from database, what would be the id of rows following the deleted row?
And if I update row 4, will it be given a new id or the id of row 4 will remain same after updating database?
What is the difference between autoincrement and autoincrement not null? Official documentation says that it will degrade the performance and database will work slow. Is it mandatory to write it along with integer primary key?
I have read many stackoverflow answers and also read SQLite Official Documentation but I couldn't get it.
Assuming id is the name of your INTEGER PRIMARY KEY column.
If you update a row, it will never change its id, since it is still the same row. (that's the whole point of update).
Regarding the creation of new ids, everything is in the doc your linked:
Relevant sections :
Without AUTOINCREMENT
On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually the one more than the largest ROWID currently in use.
There is no strict rule, it can be implementation dependent. Usually it will be equal to select max(id)+1. In you case 6, but if you delete row 5, the id 5 can be reused. (But don't rely on this).
With AUTOINCREMENT
If a column has the type INTEGER PRIMARY KEY AUTOINCREMENT (...) the ROWID chosen for the new row is at least one larger than the largest ROWID that has ever before existed in that same table. If the table has never before contained any data, then a ROWID of 1 is used.
Adding NOT NULL to AUTOINCREMENT is therefore pointless.
In your case, the next id is 6, no matter how many rows you delete before that.
If you just use integer primary key then any insert will use the current maximum ROWID (for which id is effectively an alias) plus one. Thus deleting row 3 and inserting a new row will use 6. However, if you were to delete row 5 (leaving the maximum ROWID at 4) a newly-inserted row would get an id of 5.
If you care about id's not being reused, then you need autoincrement. The reason this is slower (and advised against unless you need it) is that the largest ever value is kept in an internal table. Inserts then have to read/update this internal table as part of their operation. If autoincrement was used, then an insert after deleting either 3 or 5 would create a row with id 6.
A normal update of row 4 would leave the id the same, unless you used insert or replace which effectively deletes the row and inserts a new one. Such an insert would follow the same rules as above.

How do I keep my SQLite Database organized? Android

Say my SQLite Databate has 2 columns, the first being an auto-incrementing ID and the 2nd being some string. Say right now it's
1 random
2 jellybean
3 ImTired
if I were to delete entry 2, it would then be
1 random
3 ImTired
What I want is a way to make it so when you delete entry 2, it turns it into
1 random
2 ImTired
I thought about updating the entries to shift them all down one and delete the last one, but even if it worked(in my case, it deleted all of my entries, but whatever...), and even if I did get it to
1 random
2 ImTired
the next time I create a new entry, it'll be entry 4. I don't think this necessary to my app, but it seriously bugs me.
The ID column on your DB is working as a Primary Key, which is a column or group of columns used to uniquely identify a row. Once you set a Primary Key on a row you shouldn't change it, else you risk losing the consistency of the DB. For instance, suppose you later create another table that references the rows in your first table. That reference will be made using the Primary Key, and if you later change it your data won't make sense anymore.
If you wanted the ID column to keep changing just to reflect the number of rows in your table you can solve that problem with other methods. For instance. SQL offers a COUNT operator that will return the number of rows in your table:
SELECT COUNT(*) FROM Table_name;

Android SQLite dilemma

I want to increase my integer primary key by 1 for every new entry in a specific table. I read that i could do that by using AUTOINCREMENT. On the other hand i found the note below:
Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. One is the usual increment. However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence. AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential.
So my question is, shall i use autoincrement to keep counting my rows in the table?
Use AUTOINCREMENT for increasing the primary key and a simple query to count the number of rows in the table. Don't use AUTOINCREMENT to count the number of rows.
SELECT COUNT(*) FROM table_name
You should count the number of rows using
SELECT Count(*) FROM tableName
For ensuring a unique id for your rows, you can use the autoIncrement function. That is its purpose and that is what it will do.
Usually there's no need to be perfectionist with your db key index - its important that it will be unique but in 99% of the times the consistency is not important.
AUTOINCREMENT ensure your key id will be unique and that's why you should use it.
of-course I don't know the reason you want a consistent key so there is a chance you do need it but
there's other aids for anything you would think of doing with a consistent id row
in Android - for counting and going through your table you can use cursors.

Understanding android sqlite auto increment field

I have a table in my android sqlite database.There is a field 'id' which is auto increment primary key.My application service inserting a row in every minutes in the table.At some point in time i am deleting the entry older than 5 days.So total entry is limited.But id is increasing.So i am confuse about the value of the id as it is increasing with each new entry.What will be the condition when 'id' field exceed maximum value?. what is the maximum value of this field?How to work with this under the circumstances?Can i really work with auto increment field here?
The table Creation statement:
create table datausage (id integer primary key autoincrement,"
+ "date date not null,time text not null,level integer)
See this for the official docs. The largest possible ROWID is 9223372036854775807, which if you are writing 1000 entries a second will last more than 10675199116 days: I don't think you'll have difficulty with that.
When that is reached the database will start picking unused IDs, so you'll also be fine.
Use AUTO INCREMENT: it is the best option available to you.
EDIT: also, it is called AUTO INCREMENT for a reason. It is supposed to automatically increase.
An auto-incrementing primary key will continue to increment forever (in our terms) and will not replace deleted records. Even if you delete old records the id field will increment continuously.
As has been stated in other answers there is more than enough space for the field as well.
You need not use autoincrement if you want put your own values as per your logic,it will increment the value every time you insert a row.Instead you can drop table and recreate table if you want use autoincrement.

What happens to autoincrement primary keys after delete?

I'm trying to create a database file with objects and sub-objects. For example,
The first table would be of jars for storing change. Each jar entry would have a autoincrement primary ID key and data about the jar, like height, diameter or whatever.
The second table would contain the coins. Each coin would have an ID (needed?) and would also have the ID of the jar that it is contained in and it's data (like value, year, etc.).
My question is what happens to the autoincrement keys when one is deleted? Say you had 4 jars:
1
2
3
4
and you deleted number 2. Would the remaining jars be renumbered 123? or would there be a gap? If there is a gap, is it filled so the next jar created would have an ID of 2? or would the next one still be 5?
Thanks
You can read about that in the sqlite AUTOINCREMENT docs:
The usual algorithm is to give the
newly created row a ROWID that is one
larger than the largest ROWID in the
table prior to the insert. If the
table is initially empty, then a ROWID
of 1 is used. If the largest ROWID is
equal to the largest possible integer
(9223372036854775807) then the
database engine starts picking
positive candidate ROWIDs at random
until it finds one that is not
previously used. If no unused ROWID
can be found after a reasonable number
of attempts, the insert operation
fails with an SQLITE_FULL error.
In short, the after inserting 4 rows, deleting the second, and then inserting another one on the end, the ids would be:
1 3 4 5
It depends on the database implementation. For instance, MySQL inititializes the autoincrement counter at server startup and then keeps the autoincrement number in memory. So if you add 10 rows to an empty table and then delete them all the next autoincrement number would be 11 not 1.

Categories

Resources