Putting a Database Into Your First Web App, Step by Step
Many beginners build a web app that keeps data in memory or in a file, then hit a wall. This step plan adds a real database in manageable stages. The timeline is a suggestion for spare-time learners; go faster or slower as you like.
Stage 1 (days 1-2): choose and set up
- Pick SQLite for local development unless your course or host needs PostgreSQL or MySQL.
- Create a
schema.sqlwith your first table. - Check you can open the database with a tool and see the table.
Pitfall: spending a week comparing databases. Any of the common ones is fine for a first app.
Stage 2 (days 3-4): design the tables
- List the things your app stores, one table each.
- Give each table a primary key and mark required columns NOT NULL.
- Add foreign keys where tables link.
Pitfall: one huge table with everything. See designing your first relational database.
Stage 3 (days 5-6): connect from the app
- Use your framework's recommended way to connect, or the driver directly.
- Read the connection string or file path from an environment variable.
- Write one route that runs a simple SELECT and shows the result.
Pitfall: opening a new connection for every query inside a loop.
Stage 4 (week 2): build CRUD routes
Build one route per operation for your main table:
| Operation | Route example | SQL |
|---|---|---|
| Create | POST /notes | INSERT with placeholders |
| Read | GET /notes | SELECT with ORDER BY and LIMIT |
| Update | POST /notes/5/edit | UPDATE ... WHERE id = ? |
| Delete | POST /notes/5/delete | DELETE ... WHERE id = ? |
Pitfall: building SQL by gluing form values into strings. Always use placeholders.
Stage 5 (week 3): handle errors and edge cases
- Catch constraint errors and show friendly messages.
- Handle "not found" when an id does not exist.
- Wrap multi-step changes in a transaction.
Pitfall: letting database errors show as raw crash pages.
Stage 6 (week 3-4): keep changes repeatable
- Put structure changes in migration files rather than typing them by hand.
- Keep sample data in a seed file for testing.
Pitfall: your laptop's database and the deployed one drifting apart. See what a schema migration is.
Stage 7 (week 4): deploy carefully
- Check how your host handles databases. Some hosting platforms reset local files on each deploy, which would wipe a SQLite file; a hosted database may be needed there.
- Set the connection string as a secret environment variable on the host.
- Never upload real passwords in code.
The plan at a glance
| Stage | Outcome |
|---|---|
| 1 | Database exists and opens |
| 2 | Tables designed with keys |
| 3 | App reads from the database |
| 4 | Full CRUD for one table |
| 5 | Friendly errors and transactions |
| 6 | Migrations and seed data |
| 7 | Deployed with secrets outside code |
Follow the form-to-database journey in our in-depth guide while working on stages 4 and 5.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.