| 1 | import sqlite3 |
| 2 | |
| 3 | # global connection |
| 4 | conn = None |
| 5 | |
| 6 | |
| 7 | def get_connection(): |
| 8 | global conn |
| 9 | if conn is None: |
| 10 | conn = sqlite3.connect("application.db") |
| 11 | return conn |
| 12 | |
| 13 | |
| 14 | def create_database(): |
| 15 | # Connect to a single SQLite database |
| 16 | conn = get_connection() |
| 17 | cursor = conn.cursor() |
| 18 | |
| 19 | # Create Users table |
| 20 | cursor.execute( |
| 21 | """ |
| 22 | CREATE TABLE IF NOT EXISTS Users ( |
| 23 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 24 | user_id INTEGER, |
| 25 | first_name TEXT, |
| 26 | last_name TEXT, |
| 27 | email TEXT UNIQUE, |
| 28 | phone TEXT |
| 29 | ) |
| 30 | """ |
| 31 | ) |
| 32 | |
| 33 | # Create PurchaseHistory table |
| 34 | cursor.execute( |
| 35 | """ |
| 36 | CREATE TABLE IF NOT EXISTS PurchaseHistory ( |
| 37 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 38 | user_id INTEGER, |
| 39 | date_of_purchase TEXT, |
| 40 | item_id INTEGER, |
| 41 | amount REAL, |
| 42 | FOREIGN KEY (user_id) REFERENCES Users(user_id) |
| 43 | ) |
| 44 | """ |
| 45 | ) |
| 46 | |
| 47 | cursor.execute( |
| 48 | """ |
| 49 | CREATE TABLE IF NOT EXISTS Products ( |
| 50 | product_id INTEGER PRIMARY KEY, |
| 51 | product_name TEXT NOT NULL, |
| 52 | price REAL NOT NULL |
| 53 | ); |
| 54 | """ |
| 55 | ) |
| 56 | |
| 57 | # Save (commit) the changes |
| 58 | conn.commit() |
| 59 | |
| 60 | |
| 61 | def add_user(user_id, first_name, last_name, email, phone): |
| 62 | conn = get_connection() |
| 63 | cursor = conn.cursor() |
| 64 | |
| 65 | # Check if the user already exists |
| 66 | cursor.execute("SELECT * FROM Users WHERE user_id = ?", (user_id,)) |
| 67 | if cursor.fetchone(): |
| 68 | return |
| 69 | |
| 70 | try: |
| 71 | cursor.execute( |
| 72 | """ |
| 73 | INSERT INTO Users (user_id, first_name, last_name, email, phone) |
| 74 | VALUES (?, ?, ?, ?, ?) |
| 75 | """, |
| 76 | (user_id, first_name, last_name, email, phone), |
| 77 | ) |
| 78 | |
| 79 | conn.commit() |
| 80 | except sqlite3.Error as e: |
| 81 | print(f"Database Error: {e}") |
| 82 | |
| 83 | |
| 84 | def add_purchase(user_id, date_of_purchase, item_id, amount): |
| 85 | conn = get_connection() |
| 86 | cursor = conn.cursor() |
| 87 | |
| 88 | # Check if the purchase already exists |
| 89 | cursor.execute( |
| 90 | """ |
| 91 | SELECT * FROM PurchaseHistory |
| 92 | WHERE user_id = ? AND item_id = ? AND date_of_purchase = ? |
| 93 | """, |
| 94 | (user_id, item_id, date_of_purchase), |
| 95 | ) |
| 96 | if cursor.fetchone(): |
| 97 | # print(f"Purchase already exists for user_id {user_id} on {date_of_purchase} for item_id {item_id}.") |
| 98 | return |
| 99 | |
| 100 | try: |
| 101 | cursor.execute( |
| 102 | """ |
| 103 | INSERT INTO PurchaseHistory (user_id, date_of_purchase, item_id, amount) |
| 104 | VALUES (?, ?, ?, ?) |
| 105 | """, |
| 106 | (user_id, date_of_purchase, item_id, amount), |
| 107 | ) |
| 108 | |
| 109 | conn.commit() |
| 110 | except sqlite3.Error as e: |
| 111 | print(f"Database Error: {e}") |
| 112 | |
| 113 | |
| 114 | def add_product(product_id, product_name, price): |
| 115 | conn = get_connection() |
| 116 | cursor = conn.cursor() |
| 117 | |
| 118 | try: |
| 119 | cursor.execute( |
| 120 | """ |
| 121 | INSERT INTO Products (product_id, product_name, price) |
| 122 | VALUES (?, ?, ?); |
| 123 | """, |
| 124 | (product_id, product_name, price), |
| 125 | ) |
| 126 | |
| 127 | conn.commit() |
| 128 | except sqlite3.Error as e: |
| 129 | print(f"Database Error: {e}") |
| 130 | |
| 131 | |
| 132 | def close_connection(): |
| 133 | global conn |
| 134 | if conn: |
| 135 | conn.close() |
| 136 | conn = None |
| 137 | |
| 138 | |
| 139 | def preview_table(table_name): |
| 140 | conn = sqlite3.connect("application.db") # Replace with your database name |
| 141 | cursor = conn.cursor() |
| 142 | |
| 143 | cursor.execute(f"SELECT * FROM {table_name} LIMIT 5;") # Limit to first 5 rows |
| 144 | |
| 145 | rows = cursor.fetchall() |
| 146 | |
| 147 | for row in rows: |
| 148 | print(row) |
| 149 | |
| 150 | conn.close() |
| 151 | |
| 152 | |
| 153 | # Initialize and load database |
| 154 | def initialize_database(): |
| 155 | global conn |
| 156 | |
| 157 | # Initialize the database tables |
| 158 | create_database() |
| 159 | |
| 160 | # Add some initial users |
| 161 | initial_users = [ |
| 162 | (1, "Alice", "Smith", "alice@test.com", "123-456-7890"), |
| 163 | (2, "Bob", "Johnson", "bob@test.com", "234-567-8901"), |
| 164 | (3, "Sarah", "Brown", "sarah@test.com", "555-567-8901"), |
| 165 | # Add more initial users here |
| 166 | ] |
| 167 | |
| 168 | for user in initial_users: |
| 169 | add_user(*user) |
| 170 | |
| 171 | # Add some initial purchases |
| 172 | initial_purchases = [ |
| 173 | (1, "2024-01-01", 101, 99.99), |
| 174 | (2, "2023-12-25", 100, 39.99), |
| 175 | (3, "2023-11-14", 307, 49.99), |
| 176 | ] |
| 177 | |
| 178 | for purchase in initial_purchases: |
| 179 | add_purchase(*purchase) |
| 180 | |
| 181 | initial_products = [ |
| 182 | (7, "Hat", 19.99), |
| 183 | (8, "Wool socks", 29.99), |
| 184 | (9, "Shoes", 39.99), |
| 185 | ] |
| 186 | |
| 187 | for product in initial_products: |
| 188 | add_product(*product) |