Sling Academy
Home/SQLite/Executing Basic SQL Commands with SQLite CLI

Executing Basic SQL Commands with SQLite CLI

Last updated: December 06, 2024

SQLite is a popular, lightweight, and fast database engine that is commonly used for small to medium-sized applications. Its primary advantage is that it requires minimal setup, and it infamously runs in memory, making it ideal for development, testing, and small applications. This article will guide you through executing basic SQL commands using the SQLite Command Line Interface (CLI).

Setting Up SQLite CLI

Before diving into executing commands, you need to have SQLite installed on your system. If you haven’t done so yet, follow these steps to install it:

  1. Download the SQLite binaries for your operating system from the official SQLite download page.
  2. Extract the downloaded files and ensure the SQLite file is in your system's PATH for easy execution from the command line, or navigate to the directory containing the SQLite executable when running commands.

With SQLite installed, you are ready to open the CLI by executing the following command in your terminal or command prompt:

sqlite3

The above command will start a new SQLite session. You should see the SQLite prompt appearing, ready to receive your commands.

Creating a New Database

To create a new database, you can simply open the SQLite CLI and specify the database filename:

sqlite3 my_database.db

This command opens (and creates if it doesn’t exist yet) a database file named my_database.db.

Creating a Table

Once you are inside the SQLite CLI, you can start creating tables. Here’s how you can create a simple table called users:

CREATE TABLE users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL
);

The above SQL command defines a table with three columns: an auto-incrementing id, a name, and an email which is unique for each user.

Inserting Data

After creating a table, you'll likely want to add data into it. Here's how you insert a row into the users table:

INSERT INTO users (name, email) 
VALUES ('John Doe', '[email protected]');

This command adds a new user with the name 'John Doe' and email '[email protected]' to the table.

Querying Data

To retrieve data, you can perform queries such as:

SELECT * FROM users;

This command fetches all rows from the users table. The results will be displayed on your CLI screen.

Updating Data

Updating existing data is straightforward with the UPDATE statement. For example:

UPDATE users SET email = '[email protected]' WHERE name = 'John Doe';

This updates the email address of the user named 'John Doe' to '[email protected]'.

Deleting Data

To delete data from a table, use the DELETE command like so:

DELETE FROM users WHERE name = 'John Doe';

This removes all rows matching the specified criteria from the users table. Be careful with DELETE commands as they irreversibly remove data.

Exiting the CLI

Once you finish executing your commands, you can exit the SQLite CLI by typing:

.exit

or pressing Ctrl + D on your keyboard.

Conclusion

SQLite CLI is a powerful tool for performing database operations without needing a separate database server. With the commands demonstrated in this article, you can create databases, manage tables, and manipulate data effectively. As you continue learning SQLite, you'll discover more advanced functionalities that can empower your database management tasks.

Next Article: How SQLite Ensures Data Integrity

Previous Article: SQLite’s Limitations: What You Need to Know

Series: Overview of SQLite

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints