CodingNic

Databases and Persistence

SQLite and Setup

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 .db file
  • 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:

code
database.py

Step 2: Add This Code

python
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:

bash
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:

code
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 .db file 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.