I'm using SQLite to save contacts, I'm retrieving contacts from a server.
I make an SQLite database with a unique value for each contact (Phone number) as you can see here :
public void onCreate(SQLiteDatabase db) {
String CREATETABLE = "CREATE TABLE contacts ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, "+
"profil TEXT, "+
"phone TEXT UNIQUE ,"+
"show TEXT )";
db.execSQL(CREATETABLE);
}
and then I made a function to be able to insert a new contact :
public void addcontact(contacts contact){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", contact.getName());
values.put("phone", contact.getNumero());
values.put("profil", contact.getProfil());
values.put("show", contact.getShow());
try {
db.insert("contacts", // table
null, //nullColumnHack
values);
} catch (Exception e){
}
db.close();
}
But when I'm trying to insert contact already inserted with the same phone number I get an error in the log :
E/SQLiteLog﹕ (2067) abort at 15 in [INSERT INTO contacts(phone,name,show,profil) VALUES (?,?,?,?)]: UNIQUE constraint failed: contacts.phone
How Can I handle that error and show a log message for example that the contact exists already ?
Aucun commentaire:
Enregistrer un commentaire