Trigger after Insert SQLITE

Asked

Viewed 36 times

2

Hello, I have a table of activities (for Deposit or Withdrawal of a piggy bank app) is inserted ID (PK automatica), value (double) Day (Text) type (can be 0 - for deposits / 1 - withdrawals)
and eutra table calls total with a value capo(double).. \

I want to create a Trigger that after inserting in the activities table, updates the value field of the total table, which "recalculates" the total of the piggy bank (a sum of values of activities with type = "0" least the sum of the activity values with type = "1" ) (deposits - withdrawals)..

What would this Rigger look like?

db.execute('CREATE TRIGGER update_total AFTER INSERT ON atividades '
            ' BEGIN '
              'UPDATE total as t SET t.valor, VALUE = '
              '(SELECT SUM(valor) FROM atividades WHERE tipo = "0")'
             ' - (SELECT sum(valor) FROM atividades WHERE tipo = "1") '
            'END');

My unsuccessful attempt :(

Thanks in advance

  • 1

    Try: (SELECT SUM(CASE WHEN tipo = "0" THEN valor WHEN tipo = "1" THEN -valor ELSE 0 END) FROM atividades).

No answers

Browser other questions tagged

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