Overview
SQLite is a small, fast, free relational database engine. Unlike MySQL or SQL Server, SQLite has no server: the whole database (tables, data, and indexes) is stored in a single ordinary file on your computer, and programs read and write that file directly. SQLite was created by D. Richard Hipp in 2000 and is in the public domain.
SQLite is the most widely deployed database in the world. It is built into every smartphone, web browsers, Python, PHP, and countless apps. It is ideal for learning SQL, prototypes, small websites, data analysis, and app storage. In this guide you will install the sqlite3 command-line tool and build a small database.
sqlite3 program, point it at a file, and start writing SQL.
SQLite Compared with Server Databases
| SQLite | MySQL / SQL Server | |
|---|---|---|
| Setup | None; a database is just a file | Install and run a database server |
| Users and passwords | None; file permissions control access | User accounts and permissions |
| Best for | Learning, single-user apps, small sites, data files | Many simultaneous users, large applications |
| Data types | Flexible (TEXT, INTEGER, REAL, BLOB, NULL) | Strict, with many types |
| SQL | Standard SQL with a few differences | Standard SQL plus vendor extensions |
Requirements
- A Windows, macOS, or Linux computer.
- Internet access to download SQLite.
- A command line window: Command Prompt or PowerShell on Windows, Terminal on Mac and Linux.
Important Notes
- Prefer a graphical tool instead of the command line? See the SQLiteStudio guide. It is still useful to learn the command line first.
- The command-line program is named
sqlite3(with a 3) on every operating system.
SQLite Installation Instructions
Choose the tab for your operating system:
Installing the SQLite Command-Line Tools
Click the tab above for your operating system.
The guide covers these major tasks:
- Download the SQLite tools.
- Extract them to a folder (Windows).
- Add the folder to your
PATH(Windows). - Confirm with
sqlite3 --version.
Confirm the installation
In a new command line window, enter:
sqlite3 --version
You should see a version number beginning with 3., followed by a date and a long code.
sqlite3 --version prints a 3.x version number.
Beginner Example: A Library Database
You will create a database file, add two related tables, insert data, and run queries.
Step 1: Create the database file
In your command line window, move to a folder where you want to keep your practice work (for example, Documents), then start sqlite3 with a file name:
cd Documents
sqlite3 library.db
The prompt changes to sqlite>. The file library.db is created as soon as you create a table.
Step 2: Turn on readable output
Commands that start with a period are dot commands for the sqlite3 tool itself, not SQL. They don't need a semicolon:
.headers on
.mode box
This shows column names and draws query results as neat tables.
Step 3: Create tables
SQL statements end with a semicolon. Type or paste:
PRAGMA foreign_keys = ON;
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
year INTEGER,
author_id INTEGER REFERENCES authors(author_id)
);
An INTEGER PRIMARY KEY column numbers new rows automatically. PRAGMA foreign_keys = ON makes SQLite enforce the link between books and authors (it is off by default).
Step 4: Insert data
INSERT INTO authors (name, country) VALUES
('Jane Austen', 'England'),
('Gabriel Garcia Marquez', 'Colombia'),
('Toni Morrison', 'United States');
INSERT INTO books (title, year, author_id) VALUES
('Pride and Prejudice', 1813, 1),
('Emma', 1815, 1),
('One Hundred Years of Solitude', 1967, 2),
('Beloved', 1987, 3),
('Song of Solomon', 1977, 3);
Step 5: Query the data
-- Books published after 1900, newest first
SELECT title, year FROM books WHERE year > 1900 ORDER BY year DESC;
-- Join books to their authors
SELECT b.title, a.name, b.year
FROM books AS b
JOIN authors AS a ON a.author_id = b.author_id
ORDER BY a.name, b.year;
-- Count books per author
SELECT a.name, COUNT(b.book_id) AS book_count
FROM authors AS a
LEFT JOIN books AS b ON b.author_id = a.author_id
GROUP BY a.name
ORDER BY book_count DESC, a.name;
The first query returns three rows, newest first: Beloved (1987), Song of Solomon (1977), and One Hundred Years of Solitude (1967). The last query shows Jane Austen and Toni Morrison with 2 books each, and Gabriel Garcia Marquez with 1.
Step 6: Explore and exit
Try these dot commands. .tables lists the tables, .schema books shows how the books table was created, and .quit exits sqlite3:
.tables
.schema books
.quit
Your data is saved in library.db automatically. To open it again later, run sqlite3 library.db from the same folder.
library.db, SELECT COUNT(*) FROM books; returns 5.
Common Errors
- The prompt shows
...>: SQLite is waiting for the rest of a statement. Type;and press Enter. - no such table: you may have opened a different file. Run
.databasesto see which file is open. - 'sqlite3' is not recognized (Windows): the
PATHwasn't set, or you didn't open a new Command Prompt afterward.
Next Steps
- Open
library.dbin a graphical tool with the SQLiteStudio guide. - Use SQLite from Python (
import sqlite3) or PHP (PDO), with no extra software. - Read the sqlite3 command-line documentation.
- SQLite stores an entire database in a single file, with no server to install or run.
- Use
sqlite3 filename.dbto create or open a database. - Dot commands (such as
.tables) control the tool; SQL statements end with a semicolon. - Turn on
PRAGMA foreign_keys = ON;to enforce relationships between tables.