Markstar
Markstar

Reputation: 873

Trouble with insert()

I'm just starting with Android DBs and have trouble inserting data with the insert() function. I have a custom Class StudyCardSet and when I try to add it to the database, only some of the values seem to be inserted successfully (_id and title), while frontid is null:

MySQLHelper.java:

private static final String SET_CREATE = "CREATE TABLE " + TABLE_SCSET + "(" + 
        ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        TITLE + " TEXT NOT NULL, " + 
        FRONTID + " TEXT NOT NULL);";

DataSource.java:

public StudyCardSet createStudyCardSet(StudyCardSet nStudyCardSet) 
{
        // Set of study cards:  _id |   title   |   frontid 
    Log.d("mydebug", "starting creation of Set: " + nStudyCardSet.getTitle() + ": " + nStudyCardSet.getFrontID());
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.TITLE, nStudyCardSet.getTitle());
    values.put(MySQLiteHelper.FRONTID, nStudyCardSet.getFrontID());
    long insertId = database.insert(MySQLiteHelper.TABLE_SCSET, null, values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_SCSET, allSets, MySQLiteHelper.ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    StudyCardSet newSet = cursorToSet(cursor);
    Log.d("mydebug", String.valueOf(newSet.getId()) + " - newSet: " + newSet.getTitle() + ": " + newSet.getFrontID());
    cursor.close();
    return newSet;
}

The first Log.d of createStudyCardSet shows all three values correctly, but in newSet, the frontid seems to be null.

I would appreciate some help on what I'm doing wrong, I tried Google and a lot of messing around, but I'm all out of ideas.

Upvotes: 0

Views: 65

Answers (1)

laalto
laalto

Reputation: 152807

It was indeed the cursorToSet() method which only got the _id and the title. I had totally forgotten about this and never checked it again after writing it initially... @laalto: If you post it as answer I'll mark it as solution.

The cursorToSet() should transfer the columns from allSets projection to a new StudyCardSet object. Based on your comment above a column value was left out in cursorToSet().

Upvotes: 1

Related Questions