mardi 26 janvier 2016

Android. Getting 0 from an Insert in SQLite

I have a Table Notes (_Id, Title, Body, Category) and a Table Categories (_Id, Name), referencing Category to Categories._Id.

Whit this method I create a Note in my SQLite Database.

/**
 * Create a new note using the title and body provided, and associated to
 * the category with row id provided. If the note is successfully created
 * return the new rowId for that note, otherwise return  a -1 to indicate failure.
 *
 * @param title the title of the note
 * @param body the body of the note
 * @param category the category associated to the note
 * @return rowId or -1 if failed
 */
public long createNote(String title, String body, String category) {
    if (title == null || title.equals("") || body == null) {
        return -1;
    }
    else {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NOTE_KEY_TITLE, title);
        initialValues.put(NOTE_KEY_BODY, body);
        // If it has associated a category
        if (!category.equals("No Category")) {
            Cursor idCategory = fetchCategory(category);
            long catId = idCategory.getLong(idCategory.getColumnIndex("_id"));
            initialValues.put(NOTE_KEY_CAT, catId);
        // Else, it has no category
        } else {
            initialValues.put(NOTE_KEY_CAT, (Byte) null);
        }
        return mDb.insert(DATABASE_NOTE_TABLE, null, initialValues);
    }
}

When a note with category not equals to No Category is inserted, everything works correctly, and the rowId of the new Note is returned. However, when it is a note with category No Category, a 0 value is returned. Best of all is that the note is in the database (I get it with a simple select operation).

Any idea of what is happening?

Aucun commentaire:

Enregistrer un commentaire