Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with SQLITE DB
#5
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

Reply


Messages In This Thread
Help with SQLITE DB - by Tuxedo - Feb-18-2021, 07:28 PM
RE: Help with SQLITE DB - by buran - Feb-18-2021, 07:39 PM
RE: Help with SQLITE DB - by Tuxedo - Feb-18-2021, 07:43 PM
RE: Help with SQLITE DB - by buran - Feb-18-2021, 07:44 PM
RE: Help with SQLITE DB - by buran - Feb-18-2021, 08:10 PM
RE: Help with SQLITE DB - by Tuxedo - Feb-18-2021, 08:40 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020