jeudi 30 juillet 2015

Changed DB, updated DB version can't add data and can't not add data

I am working on an android application that uses two databases. Recently, I had to add a new column to one of the databases. Upon doing so, it broke my database. Installing and re-installing the application on my device did nothing, and I also updated the DB version.

Trying to insert data will net me this error:

E/SQLiteLog﹕ (1) table message_table has no column named msg_type

So, I tried taking out the "msg_type" column from the insert, and inserting data which gave me this error:

E/SQLiteLog﹕ (1299) abort at 8 in [INSERT INTO message_table(recipient,message) VALUES (?,?)]: NOT NULL constraint failed: message_table.msg_typeTEXT

Here is the oncreate:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
            COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + //msg_id
            COL_2 + " TEXT NOT NULL, " + //recipient
            COL_3 + " TEXT, " + //message
            COL_4 + "TEXT NOT NULL);"); //message type
}

and the insert class:

public boolean addMessage(String recipient, String message, String type){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    //populate message object
    contentValues.put(COL_2, recipient);
    contentValues.put(COL_3, message);
    contentValues.put(COL_4, type);

    //insert new message
    long result = db.insert(TABLE_NAME, null, contentValues);

    //check if operation was successful
    if(result == -1)
        return false;
    else
        return true;
}

How can I be getting an error for either case? I thought that it didn't recognize the new column was added from the first error, but it also doesn't like not receiving the data for that column.

Aucun commentaire:

Enregistrer un commentaire