w3resource

Understanding SQLite Database and its Features


What is SQLite?

SQLite is a lightweight, self-contained, serverless database engine that is easy to set up and use. It is an open-source relational database management system (RDBMS) implemented as a C library. Unlike traditional database systems like MySQL or PostgreSQL, SQLite does not require a dedicated server to run. Instead, it stores the entire database as a single file on the disk, making it an excellent choice for embedded systems, mobile applications, and small-to-medium-sized applications.


Key Features of SQLite

    1. Serverless: No need for a separate server process or configuration.

    2. Single Database File: The entire database is stored in a single file.

    3. Cross-Platform: Compatible with various platforms, including Windows, macOS, and Linux.

    4. Zero Configuration: No setup or configuration is required.

    5. Lightweight: Designed to be small and efficient, perfect for mobile and embedded applications.

    6. ACID Compliant: Fully supports Atomicity, Consistency, Isolation, and Durability.


Syntax (When Interacting with SQLite):

SQLite does not require a special syntax beyond standard SQL commands. The commands can be executed using SQLite CLI tools or through programming languages like Python, PHP, or Java. Here's a basic structure:

-- Creating a table
CREATE TABLE table_name (
    column1 datatype PRIMARY KEY,
    column2 datatype NOT NULL,
    column3 datatype
);

-- Inserting data into the table
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);

-- Querying data
SELECT * FROM table_name WHERE column2 = value2;

-- Updating data
UPDATE table_name
SET column2 = new_value
WHERE column1 = value1;

-- Deleting data
DELETE FROM table_name WHERE column1 = value1;

Example: Basic Operations with SQLite

Python Example: Creating a Database and Table

Code:

# Import the SQLite module
import sqlite3

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

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

# Create a table named "users"
cursor.execute("""
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY,  # Auto-incremented primary key
        name TEXT NOT NULL,      # User's name
        age INTEGER              # User's age
    )
""")

# Insert data into the table
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 25))
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Bob", 30))

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

Explanation:

    1. Import sqlite3: The built-in module sqlite3 allows Python to interact with SQLite databases.

    2. Connect to a database: If the specified file does not exist, SQLite will create a new database file.

    3. Create a cursor: Used to execute SQL commands.

    4. Execute SQL commands: Create tables, insert data, and perform other operations.

    5. Commit changes: Save the changes to the database.

    6. Close the connection: Always close the connection to free resources.


Why Use SQLite?

  • Ideal for lightweight applications like mobile apps, small websites, and embedded systems.
  • Zero installation and maintenance make it developer-friendly.
  • File-based database storage provides portability.

Practical Guides to SQLite Snippets and Examples.



Follow us on Facebook and Twitter for latest update.