Step 1: Update the Package Index
Before installing SQLite, you need to update the package index. Open a terminal and run the following command:
sudo apt update && sudo apt upgrade -y
This command will update the package index and ensure that you have the latest package information.
Step 2: Install SQLite
Once the package index is updated, you can install SQLite by running the following command:
sudo apt install sqlite3
sqlite3 --version
sqlite3 mydatabase.db
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
);
Here’s a breakdown of the syntax:
- CREATE TABLE: This is the command to create a new table.
- users: This is the name of the table.
- id INTEGER PRIMARY KEY: This defines the `id` column as an integer primary key.
- name TEXT NOT NULL: This defines the `name` column as a text field that cannot be null.
- email TEXT NOT NULL: This defines the `email` column as a text field that cannot be null.
Inserting Data
To insert data into the table, you can use the INSERT INTO command. For example:\
INSERT INTO users (name, email) VALUES ('karim ultahost', 'karim.ultahost@example.com');
This command will insert a new row into the `users` table with the name `karim ultahost` and email karim.ultahost@example.com. Note that we don’t need to specify a value for the `id` column, as it will be automatically incremented by SQLite.
Querying Data
To query data from the table, you can use the `SELECT` command. For example:
SELECT * FROM users;
This command will retrieve all rows from the `users` table. The `*` symbol is a wildcard that means “all columns”.
Here’s a breakdown of the syntax:
- SELECT: This is the command to retrieve data from a table.
- *: This is a wildcard that means “all columns”.
- FROM users: This specifies the table to retrieve data from.
Exiting the SQLite Shell
To exit the SQLite shell, you can use the `.quit` command:
quit
This will close the SQLite shell and return you to the terminal.
By following these steps, you can create databases, and tables, insert data, and query data using SQLite on Ubuntu 22.04.
Read also How to Install PostgreSQL on Ubuntu.
Key Features of SQLite
SQLite is a powerful, self-contained relational database management system that offers numerous features, including:
- Zero-Configuration: SQLite requires no configuration or setup, making it easy to use and start developing applications immediately. This feature eliminates the need for complex configuration options and reduces development time.
- Serverless: SQLite does not require a separate server process, reducing overhead and complexity. This serverless architecture allows for faster development and deployment of applications on small devices or embedded systems.
- File-Based: SQLite stores data in a single file, making it easy to manage and transfer. This file-based approach allows for easy backup and recovery of data, as well as seamless migration between systems and platforms.
- ACID Compliance: SQLite follows atomicity, consistency, isolation, and durability (ACID) principles to ensure data integrity. This ensures that database transactions are processed reliably and securely, even in the event of hardware or software failures.
- SQL Support: SQLite supports standard SQL syntax and data types, making it easy to learn and use for developers familiar with SQL. SQLite also supports advanced SQL features such as views, triggers, and indexes.
- Cross-Platform: SQLite is available on multiple platforms, including Windows, macOS, and Linux, making it easy to develop and deploy applications on different operating systems. This cross-platform support allows for easy collaboration between developers who work on different platforms.
- Small Footprint: SQLite has a small binary size, making it suitable for embedded systems and mobile devices with limited storage space. The small footprint also allows for faster loading and execution times.
- High-Performance: SQLite is optimized for performance, with fast query execution and data retrieval. Its efficient database engine minimizes overhead and maximizes throughput, making it suitable for high-performance applications.
- Transactions: SQLite supports transactions, allowing for atomic updates and rollbacks. This feature ensures data integrity and consistency and improves performance by batching multiple operations together.
- Open-Source: SQLite is open-source, with a permissive license that allows for free use and modification. This open-source nature enables community-driven development and support, with a large and active community of developers contributing to the SQLite project.