Reputation: 817
I am fetching and storing phone numbers and contact names in a SQLite DB from an Android phone. Now my problem is that whenever I refresh/reload the app the SQL entries (phone and contacts) are inserted again and again giving rise to duplicate entries. How to stop this, I am using Phonegap, by the way!
I am using this simple code to populate the DB
tx.executeSql('CREATE TABLE IF NOT EXISTS details (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone TEXT)');
But it is causing double entries.
Upvotes: 1
Views: 3248
Reputation: 2004
It will solve the duplicate entry problem:
sampleDB.execSQL("INSERT OR REPLACE INTO measure_table (measure) " + "VALUES ( '" + "Length" + "')");
Upvotes: 0
Reputation: 3701
Stopping this can be as easy as defining a two field primary key, like this :
CREATE TABLE contacts(
name CHAR(10) NOT NULL,
address INTEGER,
phone INTEGER NOT NULL,
song VARCHAR(255),
PRIMARY KEY (name, phone)
)
This key will ensure no entry in the database has the same name and phone.
Hope this helps !
Upvotes: 5