2

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.

4

1 回答 1

4

Perhaps it's time to update your development kit.

The method with the signature used in the code was introduced with API Level 11:

public static long queryNumEntries (SQLiteDatabase db, String table, String selection, String[] selectionArgs)

API Level 1 had only the two-argument method:

public static long queryNumEntries (SQLiteDatabase db, String table)
于 2012-06-26T18:18:22.310 回答