Spaces:
Sleeping
Sleeping
File size: 1,321 Bytes
e184c79 | 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 | 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()
|