javaandroiddatabasesqlite

Update a simple database SQLite Android


In my android application, I have 10 imageview and 10 buttons, initially all are having light color and when I touch a button, the imageview turns into a dark color. In my SQLite I have 10 columns, every column with an Integer value, 0 for light and 1 for dark.

I'm a beginner in sqlite Please tell me how to update tables in sqlite in detail , I've seen a few tutorial and I know how to create my tablet, what i really don't understand is how to update it.


Solution

  • Let's assume you have an account object (account name, user, password etc) that you want to update. You also have a unique key (rowId). Then the update method could look like this:

    public void updateAccount( AccountObject inAccountObj){
    
        ContentValues values = new ContentValues();
        values.put(COL_NAME, inAccountObj.getName() );
        values.put(COL_USER, inAccountObj.getUser() );
        values.put(COL_PW, inAccountObj.getPassword() );
        values.put(COL_NOTES, inAccountObj.getNotes() );
    
        String whereClause = KEY_ROWID + " = ? ";
        String[] whereArgs = new String[] { inAccountObj.getRowId().toString() };
    
        mDb.update(TABLE_ACCOUNT, values, whereClause, whereArgs);
    
    }
    

    To sum up: you handover the data to content values and the update the row in the database table using the unique key.