-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_setup.py
More file actions
53 lines (46 loc) · 1.58 KB
/
db_setup.py
File metadata and controls
53 lines (46 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sqlite3
import os
def create_database():
# Create database if it doesn't exist
if not os.path.exists('finance_tracker.db'):
conn = sqlite3.connect('finance_tracker.db')
cursor = conn.cursor()
# Create transactions table
cursor.execute('''
CREATE TABLE transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
amount REAL NOT NULL,
category TEXT NOT NULL,
description TEXT,
transaction_type TEXT NOT NULL
)
''')
# Create categories table
cursor.execute('''
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
type TEXT NOT NULL
)
''')
# Insert default categories
default_categories = [
('Salary', 'income'),
('Freelance', 'income'),
('Groceries', 'expense'),
('Rent', 'expense'),
('Utilities', 'expense'),
('Entertainment', 'expense'),
('Transportation', 'expense'),
('Savings', 'saving'),
('Investment', 'saving')
]
cursor.executemany('INSERT INTO categories (name, type) VALUES (?, ?)', default_categories)
conn.commit()
conn.close()
print("Database created successfully!")
else:
print("Database already exists!")
if __name__ == "__main__":
create_database()