SQLite – 25 – SQLite Command-Line Interface (CLI)

SQLite Command-Line Interface (CLI) is a powerful and versatile tool that allows users to interact with SQLite databases directly from the command line or terminal. It provides a convenient way to perform a wide range of database-related tasks, including creating, querying, and managing SQLite databases. In this discussion, we’ll explore the SQLite CLI, its basic usage, common commands, and practical examples.

Launching the SQLite CLI:

To start using the SQLite CLI, you need to have SQLite installed on your system. Most Unix-like operating systems (including Linux and macOS) come with SQLite pre-installed. On Windows, you can download the SQLite CLI from the official website (sqlite.org).

To launch the SQLite CLI, open your command prompt or terminal and enter the following command:

sqlite3

This will start the SQLite CLI and display a prompt where you can enter SQLite commands and SQL queries.

Basic SQLite CLI Commands:

The SQLite CLI provides a variety of commands and features to interact with databases. Here are some of the most commonly used commands:

  1. .open: Open or create a database file. .open mydatabase.db
  2. .tables: List all tables in the current database. .tables
  3. .schema: Show the schema (table structure) of a table. .schema tablename
  4. .mode: Set the output mode (e.g., CSV, column, line). .mode csv
  5. .headers: Toggle column headers on or off in query results. .headers on
  6. .import: Import data from a CSV file into a table. .import data.csv mytable
  7. .output: Redirect query results to a file. .output output.txt
  8. .quit or .exit: Exit the SQLite CLI. .quit

Practical Examples:

Let’s walk through some practical examples of using the SQLite CLI:

1. Creating a Database:

To create a new SQLite database, use the .open command followed by the desired database filename. If the database does not exist, it will be created.

.open mydatabase.db

2. Creating a Table:

You can create a new table using standard SQL commands. For example, to create a Customers table with ID, FirstName, and LastName columns, you can execute the following SQL command:

CREATE TABLE Customers ( ID INTEGER PRIMARY KEY, FirstName TEXT, LastName TEXT );

3. Inserting Data:

Use the INSERT INTO SQL command to add data to your table. For instance, to insert a new customer, you can run:

INSERT INTO Customers (FirstName, LastName) VALUES ('John', 'Doe');

4. Querying Data:

You can retrieve data from your database using standard SQL queries. To retrieve all records from the Customers table, use:

SELECT * FROM Customers;

5. Importing Data:

The .import command allows you to import data from a CSV file into a table. Suppose you have a CSV file named data.csv with customer information. To import it into the Customers table, use:

.import data.csv Customers

6. Exporting Data:

You can export query results to a file using the .output command. For instance, to save the results of a query to a text file named results.txt, you can do:

.output results.txt SELECT * FROM Customers; .output stdout

7. Quitting the SQLite CLI:

To exit the SQLite CLI, simply type:

.quit

Conclusion:

The SQLite Command-Line Interface (CLI) is a versatile tool for managing SQLite databases directly from the command line or terminal. It provides a user-friendly environment for creating, querying, and managing databases. Whether you’re a developer, database administrator, or just need to perform ad-hoc database tasks, the SQLite CLI is a valuable resource for interacting with SQLite databases efficiently. Its simplicity and convenience make it a popular choice for working with SQLite databases in various contexts.