From 88c5c9fabf5aeb3ebbc378aa1e983c0123e70bcb Mon Sep 17 00:00:00 2001 From: Khaled Mahfouz Date: Thu, 10 Jul 2025 13:27:11 +0300 Subject: [PATCH] moving the database file to databases/ dir - version : 1.7 - description : moving the database file to databases/ dir and updating init-db() function to use the databases/ dir as the default . //############################################// modified: .gitignore modified: app.py //############################################// --- .gitignore | 1 + app.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index fc18853..e1ba161 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ *.db +databases diff --git a/app.py b/app.py index 281d744..344912c 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import sqlite3 import csv import io import re +import os # Import the os module from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory import datetime @@ -10,13 +11,22 @@ app = Flask(__name__) app.secret_key = 'your_super_secret_key_12345' app.config['PHONE_REGEX'] = re.compile(r'^09\d{8}$') # Syrian phone format +# Define the path to the database folder +# This creates a path like 'your_project/databases/students.db' +DATABASE_FOLDER = os.path.join(app.root_path, 'databases') # Use app.root_path for reliable directory +DATABASE_FILE = os.path.join(DATABASE_FOLDER, 'students.db') + # --- Database Functions --- def get_db_connection(): - conn = sqlite3.connect('students.db') + # Ensure the database folder exists before connecting + os.makedirs(DATABASE_FOLDER, exist_ok=True) + conn = sqlite3.connect(DATABASE_FILE) # Connect to the specified path conn.row_factory = sqlite3.Row return conn def init_db(): + # Ensure the database folder exists before creating the DB file + os.makedirs(DATABASE_FOLDER, exist_ok=True) with get_db_connection() as conn: conn.execute('DROP TABLE IF EXISTS students') conn.execute(''' @@ -37,6 +47,7 @@ def init_db(): points INTEGER DEFAULT 0 NOT NULL ) ''') + # Add indexes for faster search conn.execute('CREATE INDEX idx_student_name ON students(student_name)') conn.execute('CREATE INDEX idx_parent_name ON students(parent_name)') print("Database initialized with schema constraints and indexes") @@ -466,4 +477,4 @@ def delete_student(student_id): return redirect(url_for('index')) if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + app.run(debug=True)