Tutorials Logic, IN info@tutorialslogic.com

Python SQLite Database: Store Data in a Local File

SQLite Database

SQLite is a small database stored in a local file.

Python includes sqlite3, so you can practice database storage without installing a server.

Use SQLite for learning SQL, small apps, local tools, prototypes, and project data that should survive after the program stops.

Connection and Table

A connection opens the database file. SQL statements create tables and query data.

  • Use a with block so the connection is closed cleanly.
  • Create tables before inserting rows.
  • Commit changes so inserts and updates are saved.

Create Table

Create Table
import sqlite3

with sqlite3.connect("tasks.db") as conn:
    conn.execute("""
        CREATE TABLE IF NOT EXISTS tasks (
            id INTEGER PRIMARY KEY,
            title TEXT NOT NULL
        )
    """)

The database file is created if it does not exist.

Insert and Select

Use parameter placeholders for values instead of joining strings into SQL.

  • Use ? placeholders for user or variable data.
  • Fetch rows after SELECT queries.
  • Keep SQL readable with clear table and column names.

Save and Read Task

Save and Read Task
import sqlite3

with sqlite3.connect(":memory:") as conn:
    conn.execute("CREATE TABLE tasks (title TEXT)")
    conn.execute("INSERT INTO tasks (title) VALUES (?)", ("Learn SQLite",))

    rows = conn.execute("SELECT title FROM tasks").fetchall()
    print(rows[0][0])
Output
Learn SQLite

The value is inserted safely through a placeholder and then read back.

Before you move on

Can You Use SQLite Database?

5 checks
  • You can create a SQLite connection.
  • You can create a table with SQL.
  • You can insert values using placeholders.
  • You can select rows and read results.
  • You can choose SQLite when a local file database is enough.

SQLite Decisions

0 of 2 checked

Q1. Why use placeholders in SQL statements?

Q2. What is SQLite especially useful for in beginner projects?

Database Write Traps

  • Building SQL with string concatenation

    Use placeholders for values to avoid broken queries and unsafe input handling.
  • Forgetting to save changes

    Use a with block or call commit after inserts, updates, and deletes.
  • Using SQLite as a full server replacement

    Use it for local and small apps. Choose a database server for heavy multi-user workloads.

Try this next

Store Rows Locally

0 of 3 completed

  1. Create a notes table with title and body columns, insert two rows, and select them.
  2. Ask for a task title and search using a ? placeholder.
  3. Alter the table design in a fresh script so each task can be complete or pending.

Questions About SQLite Database

For basic Python use, no. The sqlite3 module is included with Python.

Yes. It is excellent for local task apps, notes apps, small reports, and learning SQL.

Placeholders keep values separate from SQL syntax, which avoids many quoting mistakes and unsafe query patterns.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.