How to Create Your First SQLite Database and Table

SQLite is the friendliest database to learn with: no server to install, no passwords, and the whole database is a single file. This how-to gets you from nothing to a table with data in it.

What you need

macOS and most Linux systems include the sqlite3 command already. On Windows, download the command-line tools from the official SQLite website, or install a graphical tool if you prefer clicking to typing.

Steps

  1. Open a terminal (Command Prompt or PowerShell on Windows) in a folder where you keep practice work.
  1. Create and open a database file:
sqlite3 library.db

If library.db does not exist, SQLite creates it. You will see a sqlite> prompt.

  1. Create a table:
CREATE TABLE books (
  id     INTEGER PRIMARY KEY,
  title  TEXT NOT NULL,
  author TEXT,
  year   INTEGER
);

End every SQL statement with a semicolon. Without it, SQLite waits for more input.

  1. Add some rows:
INSERT INTO books (title, author, year) VALUES ('Gardens of Rain', 'Ada North', 2019);
INSERT INTO books (title, author, year) VALUES ('Paper Moon', 'Ben Okoro', 2022);

You do not give an id; SQLite fills it in.

  1. Read them back:
SELECT * FROM books;
  1. Make the output easier to read with two SQLite settings:
.headers on
.mode column

Commands starting with a dot are instructions to the SQLite tool itself, not SQL.

  1. Leave with .quit. Your data is saved in library.db.

How to check it worked

Common problems

ProblemLikely causeFix
Nothing happens after a statementMissing semicolonType ; and press Enter
"no such table: books"You opened a different file, or misspelt the nameCheck the folder and file name
"NOT NULL constraint failed"You left out the titleEvery book needs a title
"command not found: sqlite3"Tool not installed or not on your pathInstall it, or use a graphical tool

Next steps

Try a few SELECT queries from SELECT queries in five short modules, then connect to this same file from a Python script. When you want a fresh start, simply delete library.db and begin again; that freedom is exactly why SQLite is great for learning.

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.

Related pages on this site