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.

7
  • what fields does the table have? Commented Jun 26, 2012 at 17:58
  • Please post the logcat errors. Commented Jun 26, 2012 at 17:59
  • This can be written using a single SQL[ite] query: no need to check the results. Try searching for "the intent" on SO. Commented Jun 26, 2012 at 18:01
  • I get The method queryNumEntries(SQLiteDatabase, String) in the type DatabaseUtils is not applicable for the arguments (SQLiteDatabase, String, String, String[]) Commented Jun 26, 2012 at 18:04
  • That's a compile time error that says: I take (SQLiteDatabase, String), but I was given (SQLiteDatabase, String, String, String[]). Time to go back to the API. Commented Jun 26, 2012 at 18:08

1 Answer 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.