How to search a table by date and add values contained in it?

Asked

Viewed 573 times

1

It’s the following guys, I’m having a lot of trouble creating a search by date in my android sqlite database.
I wanted to search the tables by date, if possible, I wanted to make a condition that would detect the current date, and only update the table (adding certain values) not needing to create new tables.

From now on I thank greatly to those who give me a "light" on this question.

My table is this:

CREATE TABLE IF NOT EXISTS ENTRADA (

    _id                INTEGER       NOT NULL
                                     PRIMARY KEY AUTOINCREMENT,
    DATA                DATE         NOT NULL DEFAULT CURRENT_DATE,
    SALDO               VARCHAR (20),
    DEPOSITO            VARCHAR (20),
    ECONOMIAS           VARCHAR (20),
    SALARIODOMES        VARCHAR (20),
  • What do you mean? You want to check the field data is equal to the current date?

  • Yes, I want to check if the date field is the current date (why CURRENT_DATE, alias, this table I created will always be created with the current date even? I’m sorry about the other question, but I started messing with sql a very short time ago).

  • Dai, wanted to create a function that checked by date (can be the current or other date), and summed the BALANCE fields.. alias, I wanted to create only one table by date(day)... hence the sum would refer to the date of the table (this will always be done updating the same table right? How do I do that too? kkkk, sorry for my lack of experience on the subject)

2 answers

1

  • It would be something like that, Eric. How do I take the value that was previously in the BALANCE field, and add it to the new value that I will be updating in the table?

  • "Select all tables with the current date, and add their VALUE fields".. Is there also a way to execute this statement? If so, how would it look in SQL?

  • Thanks for the answer, the link helped me more about the commands in SQL :D

0

Use the Calendar Class to get and record the day, month and year. Convert to String and record as String in the database. Use double for the value (non-comma point). "Create table..._id INTEGER...d(for the day) text + m(mes).. y(year) all "TEXT", the value leave in "double" in the table creation

//In the Class you will search in use

private SQLiteDatabase database;

//afterward...

public void somando() {
   Cursor cursor;
cursor = database.rawQuery("SELECT SUM(valor) FROM nomeDaSuaTabela WHERE d = ? AND m = ? AND y = ?",new String[] { dia, mes, ano });
//obtenha do sistema os valores da data ou de um EditText 
        if (cursor.moveToFirst()) {
                    double total = cursor.getDouble(0);// o argumento é o int
                                                        // indice da coluna

                    mensagemExibir(
                            "Total",
                            "A Soma do Valor doQueVcQuer do dia "
                                    + dia
                                    + " de "
                                    + mes
                                    + " de "
                                    + ano
                                    + " é de\n R$ "
                                    + String.valueOf(String.format("%.2f",
                                            total)));

                }
public void mensagemExibir(String titulo, String texto) {
        AlertDialog.Builder mensagem = new AlertDialog.Builder(
                SuaClasseContexto.this);
        mensagem.setTitle(titulo);
        mensagem.setMessage(texto);
        mensagem.setNeutralButton("OK", null);
        mensagem.show();
        mensagem.setCancelable(true);

    }

play the method by adding() into the event of a Button. In the message you can use a Numberformat.getCurrencyInstance(). Although if you edit after any error in typing the value android usually round the doubles after the point. There you have to roll more if you want a lot of accuracy. Abs

  • Thanks for the reply, clarified me of how to use the SUM (was using a while to make the sum :P).

Browser other questions tagged

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