Skip to content

Develop prince #22

Merged
merged 3 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/backend/animal_shelter.db
/backend/test_animal_shelter.db
/frontend/node_modules

# ignore sqlite db file
animal_shelter.db
43 changes: 43 additions & 0 deletions backend/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sqlite3
import os

def init_db():
"""Initialize the database, creating tables and inserting initial data."""
db_path = os.path.join(os.path.dirname(__file__), 'animal_shelter.db')

if os.path.exists(db_path):
print("Database already initialized.")
return

# Connect to SQLite database (it will create the file if it doesn't exist)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Create tables
cursor.execute('''CREATE TABLE IF NOT EXISTS animals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
species TEXT NOT NULL,
breed TEXT,
age INTEGER,
personality TEXT,
image_path TEXT,
adoption_status TEXT DEFAULT 'Available'
)''')

cursor.execute('''CREATE TABLE IF NOT EXISTS adopters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE,
join_date TEXT DEFAULT CURRENT_TIMESTAMP
)''')

# Insert initial data (e.g., a test animal)
cursor.execute('''INSERT INTO animals (name, species, breed, age, personality, image_path)
VALUES (?, ?, ?, ?, ?, ?)''',
('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg'))

conn.commit()
conn.close()
print("Database initialized and initial data inserted.")
Loading