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 //############################################//
هذا الالتزام موجود في:
1
.gitignore
مباع
1
.gitignore
مباع
@@ -1,2 +1,3 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
*.db
|
*.db
|
||||||
|
databases
|
||||||
|
|||||||
13
app.py
13
app.py
@@ -2,6 +2,7 @@ import sqlite3
|
|||||||
import csv
|
import csv
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
|
import os # Import the os module
|
||||||
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory
|
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@@ -10,13 +11,22 @@ app = Flask(__name__)
|
|||||||
app.secret_key = 'your_super_secret_key_12345'
|
app.secret_key = 'your_super_secret_key_12345'
|
||||||
app.config['PHONE_REGEX'] = re.compile(r'^09\d{8}$') # Syrian phone format
|
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 ---
|
# --- Database Functions ---
|
||||||
def get_db_connection():
|
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
|
conn.row_factory = sqlite3.Row
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def init_db():
|
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:
|
with get_db_connection() as conn:
|
||||||
conn.execute('DROP TABLE IF EXISTS students')
|
conn.execute('DROP TABLE IF EXISTS students')
|
||||||
conn.execute('''
|
conn.execute('''
|
||||||
@@ -37,6 +47,7 @@ def init_db():
|
|||||||
points INTEGER DEFAULT 0 NOT NULL
|
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_student_name ON students(student_name)')
|
||||||
conn.execute('CREATE INDEX idx_parent_name ON students(parent_name)')
|
conn.execute('CREATE INDEX idx_parent_name ON students(parent_name)')
|
||||||
print("Database initialized with schema constraints and indexes")
|
print("Database initialized with schema constraints and indexes")
|
||||||
|
|||||||
المرجع في مشكلة جديدة
حظر مستخدم