Current Location: Home> Latest Articles> How to Easily Connect to SQLite Database Using PDO and Perform Operations

How to Easily Connect to SQLite Database Using PDO and Perform Operations

M66 2025-06-19

How to Easily Connect to SQLite Database Using PDO and Perform Operations

Databases are core technologies in web application development. SQLite, as a lightweight embedded database engine, is widely used in various projects. This article will introduce how to use PHP's PDO (PHP Data Object) extension to connect to an SQLite database and demonstrate how to perform data operations.

PDO is a database access layer in PHP that provides a unified interface for multiple database types, including SQLite. Using PDO, developers can easily switch between different databases without changing much code, significantly improving development efficiency. Let's take a look at how to connect to an SQLite database using PDO.

1. Setting Up the Environment

First, ensure that your server environment has SQLite installed and the PDO extension enabled. If your server doesn't have SQLite installed, you need to configure it and make sure the PDO extension is enabled.

Next, create an SQLite database file. You can use the SQLite command-line tool or other database management tools to create an empty database file.

2. Writing PHP Code to Connect to SQLite Database

Once you have the database file ready, you can start writing PHP code to connect to the SQLite database. Here is a simple example to connect to an SQLite database:

<?php
try {
    // Connect to SQLite database
    $pdo = new PDO("sqlite:/path/to/database.db");
    
    // Set error reporting mode to exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Execute query
    $statement = $pdo->query("SELECT * FROM users");

    // Iterate through result set
    while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
        echo "ID: " . $row['id'] . ", Name: " . $row['name'] . "<br>";
    }
} catch (PDOException $e) {
    // Handle exceptions
    echo "Error occurred while connecting to the database: " . $e->getMessage();
}
?>
  

In this example, we first use the `new PDO()` function to create a PDO object and pass the path to the SQLite database file. Then, we use the `setAttribute()` method to set the error reporting mode to exceptions so that PHP will throw an exception in case of errors.

Next, we use the `query()` method to execute a simple query and iterate through the result set, outputting the `id` and `name` fields for each row.

Note that you should replace `/path/to/database.db` with the actual path to your SQLite database file in your application.

3. Performing Database Operations

In addition to queries, you can also use PDO to perform insertions, updates, and deletions. Below is an example with `INSERT`, `UPDATE`, and `DELETE` operations:

<?php
try {
    // Connect to SQLite database
    $pdo = new PDO("sqlite:/path/to/database.db");
    
    // Set error reporting mode to exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Insert data
    $pdo->exec("INSERT INTO users (name) VALUES ('John')");
    
    // Update data
    $pdo->exec("UPDATE users SET name = 'Jane' WHERE id = 1");
    
    // Delete data
    $pdo->exec("DELETE FROM users WHERE id = 1");
} catch (PDOException $e) {
    // Handle exceptions
    echo "Error occurred while connecting to the database: " . $e->getMessage();
}
?>
  

In this example, we use the `exec()` method to perform `INSERT`, `UPDATE`, and `DELETE` operations. Note that the `exec()` method is used for SQL statements that do not return a result set, such as `INSERT`, `UPDATE`, and `DELETE`.

Summary

This article covered how to connect to an SQLite database using PDO and provided related code examples. With PDO, developers can easily connect to and interact with an SQLite database. Whether performing queries, insertions, updates, or deletions, using PDO simplifies database operations, improving development efficiency.

We hope this article helps you better understand and use PDO for SQLite database operations and enhances your PHP development skills.