Spaces:
Sleeping
Sleeping
| import sqlite3 | |
| import os | |
| from werkzeug.security import generate_password_hash | |
| # Define the path to the database (portable: same directory as script) | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| DB_PATH = os.path.join(BASE_DIR, 'cancer_detection.db') | |
| # Function to initialize the database | |
| def init_db(): | |
| if os.path.exists(DB_PATH): | |
| print("Database already exists. Skipping initialization.") | |
| return | |
| # Connect to the SQLite database (or create it if it doesn’t exist) | |
| conn = sqlite3.connect(DB_PATH) | |
| cursor = conn.cursor() | |
| # Create the users table | |
| cursor.execute(''' | |
| CREATE TABLE users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT UNIQUE NOT NULL, | |
| password TEXT NOT NULL | |
| ) | |
| ''') | |
| # Create the uploads table | |
| cursor.execute(''' | |
| CREATE TABLE uploads ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| user_id INTEGER NOT NULL, | |
| upload_date TEXT NOT NULL, | |
| image_path TEXT NOT NULL, | |
| result TEXT NOT NULL, | |
| FOREIGN KEY (user_id) REFERENCES users (id) | |
| ) | |
| ''') | |
| # Commit and close the connection | |
| conn.commit() | |
| conn.close() | |
| print("Database initialized successfully.") | |
| # Run the init_db function if this script is executed | |
| if __name__ == '__main__': | |
| init_db() | |