Databases and Persistence
12 min read
SQLite and Setup
SQLite and Setup
Now it’s time to create your first real database.
You will create a database file and connect to it using Python.
What is SQLite?
SQLite is a simple database that stores data in a file.
You don’t need to install anything.
- It runs locally
- It creates a
.dbfile - It is perfect for beginners
What You Will Do
You will:
- Create a database file
- Connect to it using Python
- Verify that it works
Step 1: Create a New File
In your project folder, create a file:
database.py
Step 2: Add This Code
import sqlite3
# connect to database (this creates the file if it doesn't exist)
conn = sqlite3.connect("database.db")
print("Database created and connected successfully")
conn.close()
Step 3: Run the File
In your terminal:
python database.py
What Just Happened?
- Python created a file called
database.db - This file is your database
- You successfully connected to it
Check your folder—you should now see:
database.db
Important Concept
Your database is now:
- A real file
- Stored on your computer
- Ready to store data
Why This Matters
Before:
- Your app forgot everything
Now:
- You have a place to store data
This is the first step toward persistence.
Common Mistakes
- ❌ Forgetting to run the file
- ❌ Looking for the
.dbfile before running - ❌ Typos in the filename
Summary
- SQLite creates a database file
- You connected using Python
- Your app now has storage
In the next lesson, you will create your first table.