Update Android Database

-1

I am creating a database on Android that has a column called status where it is saving (0 or 1) 0 for when it is without internet and 1 for when it has internet, I want to do the update of the bank for the following to occur, when to return the internet in the cell in the status column what is saved with 0 pass to is with 1! in SQL language would look like this:

update nomeTabela set status='1' where status='0' 

    
asked by anonymous 03.03.2015 / 05:43

1 answer

2

If it is just to run that command you could do something similar to what is in the Insert:

public int update(Uri uri, ContentValues contentValues, String selection,
                      String[] selectionArgs) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();

    int rowsUpdated = 0;

    rowsUpdated = sqlDB.update(DATABASE_TABLE,
                    contentValues, selection, selectionArgs);

    getContext().getContentResolver().notifyChange(uri,null);
    return rowsUpdated;
}

The method call should already come with the pointer positioned in the record you want to update. This is done by selecting the URI with a contentResolver ().

My role is a saveState, I'll post it, maybe it helps, but I recommend you better understand how ContentProvider works, as this update you've always proposed will update all the records in your table. Is this the expected behavior?

private void saveState() {
        String categoria = (String)mCategoria.getSelectedItem();
        String descricao = (String)mDescricaoText.getText().toString();
        String valor = mValorText.getText().toString();
        String parcelas = mParcelasText.getText().toString();
        String dataInclusao = mDataInclusao.getText().toString();
        String dataVencimento = mDataVencimento.getText().toString();

        if(descricao.length() == 0 && valor.length() == 0){
            return;
        }

        ContentValues values = new ContentValues();
        values.put(LancamentoTable.COLUMN_DESCRICAO, descricao);
        values.put(LancamentoTable.COLUMN_VALOR, valor);
        values.put(LancamentoTable.COLUMN_PARCELAS, parcelas);
        values.put(LancamentoTable.COLUMN_ID_CATEGORIA, categoria);
        values.put(LancamentoTable.COLUMN_DATA_INCLUSAO, dataInclusao);
        values.put(LancamentoTable.COLUMN_DATA_VENCIMENTO, dataVencimento);
        values.put(LancamentoTable.COLUMN_SINAL, (categoria.toString().equals("DEBITO") ? "-1" : "1"));

        if(todoUri==null){
            todoUri = getContentResolver().insert(SmartBillContentProvider.CONTENT_URI, values);
        } else {
            getContentResolver().update(todoUri, values, null, null);
        }
    }

My code is a beginner, I do not have much experience with Android, so I recommend the link below as a good base for working with SQLite on Android: link

    
03.03.2015 / 11:57