text_demo / database.py
Ahmed12322's picture
Update database.py
96cd7bb verified
Raw
History Blame Contribute Delete
1.38 kB
import sqlite3
# Initialize the database and create tables if they don't exist
def initialize_database():
conn = sqlite3.connect("tax_data.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS tax_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
income REAL NOT NULL,
currency TEXT NOT NULL,
tax_paid REAL NOT NULL,
tax_year INTEGER NOT NULL
)
""")
conn.commit()
conn.close()
# Function to save tax history
def save_tax_history(user_id, income, currency, tax_paid, tax_year):
initialize_database() # Ensure table exists before inserting data
conn = sqlite3.connect("tax_data.db")
cursor = conn.cursor()
cursor.execute("""
INSERT INTO tax_records (user_id, income, currency, tax_paid, tax_year)
VALUES (?, ?, ?, ?, ?)
""", (user_id, income, currency, tax_paid, tax_year))
conn.commit()
conn.close()
# Function to retrieve tax history for a user
def get_tax_history(user_id):
initialize_database() # Ensure table exists before querying
conn = sqlite3.connect("tax_data.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM tax_records WHERE user_id=?", (user_id,))
records = cursor.fetchall()
conn.close()
return records