w3resource

Step-by-Step Guide to installing SQLite3 on All Platforms


How to Install SQLite3: A Step-by-Step Guide

SQLite3 is a lightweight, self-contained database engine widely used for local storage in various applications. It is easy to install and requires no server setup. This guide provides installation instructions for SQLite3 on Windows, macOS, and Linux. By the end of this tutorial, you will have SQLite3 installed and ready to use for database management and SQL operations.


Installing SQLite3 on Different Operating Systems

On Windows

    1. Download SQLite3

    • Visit the SQLite Downloads Page.
    • Download the Precompiled Binaries for Windows (e.g., sqlite-tools-win32-x86-xxx.zip).

    2. Extract the Files

    • Unzip the downloaded file to a directory of your choice (e.g., C:\sqlite3).

    3. Add SQLite to System PATH

    • Open System Properties > Advanced > Environment Variables.
    • Edit the Path variable in System Variables.
    • Add the directory path (e.g., C:\sqlite3).

    4. Verify Installation

    • Open Command Prompt and type:
    • sqlite3 --version
    • If installed correctly, SQLite3 will display its version.

On macOS

    1. Install via Homebrew

    • Open the Terminal and run:
    •  
      brew install sqlite
      
    • Ensure Homebrew is installed before running this command.

    2. Verify Installation

    • Type the following command:
    •  
      sqlite3 --version
      
    • The version of SQLite3 should be displayed.

On Linux

    1. Install via Package Manager

    • For Debian/Ubuntu-based distributions:
    • sudo apt update  
      sudo apt install sqlite3 
      
    • For Red Hat-based distributions:
    •  
      sudo yum install sqlite 
      

      2. Verify Installation

    • Check the version using:
    •  
      sqlite3 --version
      

Using SQLite3 After Installation

Creating a Database

    1. Open your terminal or command prompt.

    2. Run the following command to create a new database or open an existing one:

    Code:

    
    sqlite3 example.db
    

    3. You will enter the SQLite shell, where you can execute SQL commands.



Creating a Table

Code:

-- Create a table named users
CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    age INTEGER
);

Inserting Data

Code:

-- Insert a record into the users table
INSERT INTO users (name, age) VALUES ('Alice', 25);

Querying Data

Code:

-- Retrieve all records from the users table
SELECT * FROM users;

Exiting SQLite Shell

Type .exit or .quit to leave the SQLite shell.


Benefits of SQLite3

    1. Lightweight: Minimal resource requirements, making it ideal for embedded systems.

    2. No Server Required: Operates without the need for a server process.

    3. Cross-Platform: Available for Windows, macOS, and Linux.

    4. SQL Support: Executes standard SQL queries for relational data management

Example Usage with Python

Code:

# Import SQLite module
import sqlite3

# Connect to SQLite database (creates example.db if it doesn't exist)
connection = sqlite3.connect("example.db")

# Create a cursor object to execute SQL commands
cursor = connection.cursor()

# Create a table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    age INTEGER
)
""")

# Insert a record
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Bob", 30))

# Commit changes and close connection
connection.commit()
connection.close()

Explanation:

    Database Connection: Connects to the SQLite database or creates one if it doesn’t exist.

    SQL Commands: Creates a table and inserts a record.

    Resource Management: Ensures the connection is properly closed after use.

Practical Guides to SQLite Snippets and Examples.



Follow us on Facebook and Twitter for latest update.