CRUD
CRUD operations (Create, Read, Update, Delete) are the foundation of most web applications. This beginner guide shows you how to build a complete data management system using SØAD's ActiveJDBC models and Handlebars templates.
Complete CRUD Example - Contact Management
| contact.py | |
|---|---|
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
CREATE TABLE contact (
id int NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
email varchar(150) NOT NULL,
phone varchar(20),
company varchar(100),
notes text,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_name (name),
KEY idx_email (email)
);
INSERT INTO contact (name, email, phone, company, notes) VALUES
('John Smith', '[email protected]', '+1-555-0101', 'Tech Solutions Inc', 'Met at conference 2024'),
('Sarah Johnson', '[email protected]', '+1-555-0102', 'Marketing Pro', 'Potential client for Q2 project'),
('Mike Davis', '[email protected]', '+1-555-0103', 'StartupXYZ', 'Interested in partnership'),
('Lisa Chen', '[email protected]', '+1-555-0104', 'Creative Agency', 'UI/UX design expert'),
('Tom Wilson', '[email protected]', NULL, NULL, 'Friend from college');
CRUD Operations Explained
- CREATE:
Contact()creates new record,save()stores it - READ:
findAll()gets all records,findById()gets one record - UPDATE:
set()updates fields,save()stores changes - DELETE:
delete()removes record from database
Simple CRUD - Todo List
| todo_list.html | |
|---|---|
| |
CREATE TABLE todo (
id int NOT NULL AUTO_INCREMENT,
task varchar(255) NOT NULL,
completed boolean DEFAULT false,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_completed (completed)
);
INSERT INTO todo (task, completed) VALUES
('Learn SØAD framework basics', false),
('Build my first CRUD application', false),
('Read the documentation', true),
('Practice with database operations', false),
('Create a real project', false);
Simple CRUD Features
- Quick Create: Add new items with simple form
- Status Toggle: Mark items as complete/incomplete
- Instant Delete: Remove items with confirmation
- Visual Feedback: Different styling for completed items
CRUD Essentials for Beginners
Create: Model() + set() + save()
Read: findAll() or findById()
Update: findById() + set() + save()
Delete: findById() + delete()
Key Tips: - Always validate user input before saving - Handle cases where records don't exist - Provide feedback to users after operations - Use redirects after POST operations to prevent duplicate submissions