Is the field CONTACT_ID in the contactcontracts table a AUTO_INCREMENT - android

I am creating a new database for contacts and keeping the contact_id of the conatctscontract field as the primary key. My question is if anyone deletes a row in the contact then will the contact_id for that contact be reused by the android system. If so then how do I prevent that contact id from being used in my application or is there a better approach to this problem?

I'm assuming your using MySQL. The database system auto_increments the contact_id primary key. When the row is deleted that id has already been "used" and is stored in the database as "used" so-to-speak, even though the row itself has been deleted. It will continue to stay that way as long as you don't reset the auto_increment starting point (which the syntax looks something like this)
ALTER TABLE contact AUTO_INCREMENT = 0;
The deleted contact_id's will not be reused if you use the auto_increment feature so long as you don't reset the auto_increment setting for that table (as far as my knowledge goes). It will only continue from the highest ID stored in the database.
Edit: If I'm not mistaken, the column using auto_increment is used in a special database statistics table (unavailable to you, but stored automatically from the database) which stores the latest "auto_incremented" number. Using the syntax above will reset the auto_increment back to 0 and it will resume from there, filling in the gaps that have been deleted.

If you are referring to the inbuilt contactsprovider database then both Contacts table and the Data table have id as AUTOINCREMENT.
So I dont think id will be reused.
You can check the codebase here

Related

How to rearrange AUTO_INCREMENT Column in SQLite (Android)

I am making an Android App in which there is a List of Users.
And for that I am having a Users table where i am maintaining users' id using AUTO_INCREMENT
db.execSQL("create table Users (u_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, u_name TEXT , u_username text)");
Now my problem is that when i am trying to delete a user from database through ListView.
But after deleting the sequence order is like this : 1,2,4,5,7
So how can i rearrange the AUTO_INCREMENT column to : 1,2,3,4,5 like this.
How to query this thing in SQLite database.
Please help Me, i am stuck.
AUTO_INCREMENT will always increment by one from the last inserted record, no matter if that record still exists or not. if a sequential ordering without gaps is what you want, auto incremental ID is not the proper way to achieve that. Add another int field where you manually keep track of this ordering.

Update database schema while preserving data (and relationships)

I have a simple database schema containing 5 tables and a few foreign key relationships. Now I want to update the schema without losing the relationships.
SQLite does not support the operation I want to perform. I want to mark a column AUTOINCREMENT. So I searched and found that I need to recreate the tables.
In all the relationships, foreign key is the auto-generated row ID. This will change when I re-insert the data (right?). And the best case scenario would be having mismatched relationships. The worst case (I think) would be having foreign keys that correspond to nothing. How do I avoid this?
One way that I can think of doing this is through dumping all data into a special model designed for this very exercise. This special model will encapsulate all the relationships. Then I can start creating new tables in order of ascending dependence, from least to most. Table that has no foreign keys (but other tables use its ID) goes in first. And on inserting rows, I update the model instance.
Is there a better way of doing this? Thank you for reading.
Values in an autoincrementing column can be set to an explicit value:
INSERT INTO MyTable(MyAutoincID, Name) VALUES(42, 'hello')
Just copy the ID together with the other column values.
Furthermore, as long as you do not enable foreign key checking with PRAGMA foreign_keys or setForeignKeyConstraintsEnabled(), your DB is allowed to have inconsistent data temporarily.

Android SQLite database removing a record

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.

data duplication in sqlite with android app

i am getting information from user in sqlite database.
But when i insert same record which is already in database it is added again.
how i can stop duplication of record in sqlite. I am developing this in android.
I am using mobile number as primary key. still it add that record in database.
Please suggest me appropriate solutions.
Thanks in advanced.
Be aware of the limitations of REPLACE or INSERT OR REPLACE as these will overwrite any custom data your app user has added to these rows in the database - it is not as advanced as UPSERT in other SQL databases.
As mentioned in a previous post you really need to identify what the primary key could be and use this information to either update old data or to remove an old row before inserting the fresh one.
If this is not possible then you could always DELETE FROM my_table or DROP my_table before running the insertions so that there will be no duplicates. This will (for better or worse) also make sure that data that is missing from new imports is not left lying around in your app.
make sure you have set your phone number as Primary Key at the time you created the table.
for example:
String query = "CREATE TABLE IF NOT EXISTS PhoneBook ("+
"TelNum VARCHAR(100) PRIMARY KEY NOT NULL,"+
"Address TEXT);";
db.execSQL(query);
and in case you want to enforce foreign keys defined in your table then call the following method before doing anything in your database
db.execSQL("PRAGMA foreign_keys = ON;"); //enforcing FK
Use REPLACE INTO keyword:
REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.

Deleting in database?

I have a question about the databases in Android. I had created in my app a database. If I delete a record from database, for example the last one, and after that I insert a new record in database, the new record is not saved in database in the row where I deleted the other record. That row is empty. It is saved in the next row. Why? Should I do something?
I'm assuming you are talking about an sqlite DB?
If your rowid is a field declared using AUTOINCREMENT, then (from the docs) 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".
You can read about the autoincrement algorithm here: http://www.sqlite.org/autoinc.html
Note that you do NOT have to use AUTOINCREMENT to have your primary key auto select a rowid.

Categories

Resources