Update Android Database

Asked

Viewed 767 times

-1

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

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

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 3

    What is your question? Edit your question and paste the code directly. The way you are is very difficult to do any kind of test.

  • Prefer codes to images!

  • Post codes, images are more complicated to analyze

1 answer

2


If it’s just to run that command you could do what’s in 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 call to the method should come already with the pointer positioned in the record you want to update. This is done by selecting the URI with a contentResolver().

My function is a saveState, I will post it, it may help you, but I recommend that you better understand how Contentprovider works, because this update you proposed will always update all the records of your table. This is 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 beginner, I have no great experience on Android, so recommend the link below as a good base to work with Sqlite on Android: http://www.vogella.com/tutorials/AndroidSQLite/article.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.