Feb-18-2021, 08:10 PM
import sqlite3 conn = sqlite3.connect('test1.sqlite3') sql="""BEGIN TRANSACTION; CREATE TABLE `transactions` ( `tdate` TEXT, `category` TEXT, `amount` INTEGER ); INSERT INTO `transactions` (tdate,category,amount) VALUES ('2020-01-01','food',1), ('2020-02-01','food',3), ('2021-01-05','food',5), ('2021-01-10','fuel`',10); COMMIT;""" conn.executescript(sql) sql = 'SELECT * from transactions' result = conn.execute(sql) for row in result: print(row) sql = """SELECT strftime('%Y', tdate) as Year, category, SUM(amount) As Amount FROM transactions GROUP BY Year, category""" result = conn.execute(sql) for row in result: print(row)
Output:('2020-01-01', 'food', 1)
('2020-02-01', 'food', 3)
('2021-01-05', 'food', 5)
('2021-01-10', 'fuel`', 10)
('2020', 'food', 4)
('2021', 'food', 5)
('2021', 'fuel`', 10)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs