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:
- Download the SQLite binaries for your operating system from the official SQLite download page.
- 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:
sqlite3The 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.dbThis 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:
.exitor 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.