Home > Backend Development > PHP Tutorial > Getting Started with SQLite3: Basic Commands

Getting Started with SQLite3: Basic Commands

William Shakespeare
Release: 2025-02-18 11:22:08
Original
755 people have browsed it

SQLite: A Comprehensive Guide to Basic Commands

SQLite is a lightweight, serverless SQL database engine implemented as a C library. Unlike client-server databases like MySQL, SQLite operates directly from disk files, eliminating the need for server requests. This makes it ideal for embedded systems and applications requiring local data storage.

Getting Started with SQLite3: Basic Commands

Key Features:

  • Serverless: Direct disk access simplifies deployment and reduces overhead.
  • Cross-platform: Portable database files work across various operating systems.
  • Lightweight: Minimal resource consumption makes it suitable for resource-constrained environments.
  • Standard SQL Compliance: Supports a significant subset of standard SQL commands.

Getting Started:

We'll use the sqlite3 command-line interface (CLI) for this tutorial. Installation instructions vary by operating system; consult the official SQLite documentation for your platform. For Debian-based systems (like Ubuntu), use:

sudo apt-get install sqlite3 libsqlite3-dev
Copy after login
Copy after login

After installation, launch the CLI by typing sqlite3 in your terminal. You'll see a prompt indicating you can type .help for assistance.

Getting Started with SQLite3: Basic Commands

Meta Commands:

Meta commands, prefixed with a dot (.), manage database settings and operations. .help lists available meta commands. Useful commands include:

  • .show: Displays current settings.
  • .databases: Lists database names and files.
  • .quit: Exits the sqlite3 program.
  • .tables: Shows existing tables.
  • .schema: Displays a table's schema.
  • .header ON/.header OFF: Toggles header display in output.
  • .mode column: Sets output mode to columns.
  • .dump: Exports the database to SQL text format.

Standard SQL Commands:

Standard SQL commands interact with the database data. They are categorized as:

  • Data Definition Language (DDL): Defines database structure.
    • CREATE TABLE: Creates a new table.
    • ALTER TABLE: Modifies an existing table (adding or renaming columns).
    • DROP TABLE: Deletes a table.
  • Data Manipulation Language (DML): Manipulates data within tables.
    • INSERT INTO: Adds new rows.
    • UPDATE: Modifies existing rows.
    • DELETE FROM: Removes rows.
  • Data Query Language (DQL): Retrieves data.
    • SELECT: Queries data from tables.

Example: A Comment Section Database

Let's create a database for a website's comment section. The table will store: post_id (auto-incrementing integer primary key), name, email, website_url (nullable), and comment.

  1. Create the database:

    sudo apt-get install sqlite3 libsqlite3-dev
    Copy after login
    Copy after login
  2. Create the table:

    sqlite3 comment_section.db
    Copy after login
  3. Insert data:

    CREATE TABLE comments (
        post_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT NOT NULL,
        website_url TEXT NULL,
        comment TEXT NOT NULL
    );
    Copy after login
  4. Select data (with headers and columnar output):

    INSERT INTO comments (name, email, website_url, comment)
    VALUES ('John Doe', 'john.doe@example.com', 'johndoe.com', 'Great article!');
    Copy after login

    Getting Started with SQLite3: Basic Commands

  5. Update data:

    .headers ON
    .mode column
    SELECT * FROM comments;
    Copy after login
  6. Delete data:

    UPDATE comments SET email = 'updated@example.com' WHERE post_id = 1;
    Copy after login
  7. Alter table (add a username column):

    DELETE FROM comments WHERE post_id = 1;
    Copy after login
  8. Drop table:

    ALTER TABLE comments ADD COLUMN username TEXT;
    Copy after login

    Conclusion:

    SQLite's simplicity and efficiency make it a powerful tool for various applications. While this tutorial covers the basics, exploring advanced features and integrating SQLite with programming languages like PHP will significantly expand its utility. GUI tools like DB Browser for SQLite can simplify database management for those preferring a visual interface.

    Frequently Asked Questions (FAQs):

    • SQLite vs. SQLite3: SQLite3 is a later version with performance improvements and enhanced features.

    • Creating a database: sqlite3 mydatabase.db

    • Creating a table: CREATE TABLE mytable (column1 type, column2 type, ...);

    • Inserting data: INSERT INTO mytable (column1, column2, ...) VALUES (value1, value2, ...);

    • Updating data: UPDATE mytable SET column1 = value WHERE condition;

    • Deleting data: DELETE FROM mytable WHERE condition;

    • Selecting data: SELECT * FROM mytable WHERE condition ORDER BY column;

    • WHERE clause: Filters results based on a condition.

    • ORDER BY clause: Sorts results by a specified column.

    • Closing a database: .quit in the sqlite3 CLI.

    Remember to replace placeholders like mydatabase.db, mytable, column1, etc., with your actual database and table names.

    The above is the detailed content of Getting Started with SQLite3: Basic Commands. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template