I have a database named contacts.db
with a table named TABLE_NAME
that has one column named Contactname
. I want to format a query to check if a contact name that I have as a value in the String variable name is already saved as a record in the table and if it isn't then I want to add it, or else do nothing. The code that I wrote is as follows:
SQLiteDatabase db = events.getReadableDatabase();
int rawNum = DatabaseUtils.queryNumEntries(db, TABLE_NAME,
"Contactname=?", new String[] {contactName});
if(rawNum==0){
addRecord(contactName);
addedCounter+=1;
}
else{
savedCounter+=1;
}
But the app is forced to close. Can you see anything wrong with the query? What I do is to see whether there are any rows and if the number of rows is 0 then to add the contact.